0

this is my code :

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
    $( document ).ready(function() {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var filename = "C:\\xampp\\htdocs\\harti\\data.txt";
        var f = fso.OpenTextFile(filename, 2, true, -1); // -1 means unicode
        f.WriteLine("Hello world!");
        f.Close();
    });
</script>

the data.txt file exists. the question is why my code does not work ? thx

Attila Naghi
  • 2,535
  • 6
  • 37
  • 59
  • 1
    ActiveX is only for Internet Explorer – Marco Acierno May 07 '14 at 11:46
  • ok, so what can i do , to make this works for mozilla &chrome ? – Attila Naghi May 07 '14 at 11:46
  • Filesystem API Should do what you want. Search it – Marco Acierno May 07 '14 at 11:49
  • 1
    It's a security breach to allow browsers to write into user's machines. Browser's script should not access and modify local file directory that belongs to the user. If so, viruses and other malicious codes will spread. – Mohammed Joraid May 07 '14 at 11:50
  • @MarcoAcierno what part of the `file API` talks about *write*? I would be the first to write nifty files into user machines! :) – Roko C. Buljan May 07 '14 at 11:51
  • `ActiveXObject` is not only IE-proprietary but also pretty much obsolete. You'd rather create a file in the memory and offer to download (see [this hack](http://stackoverflow.com/a/18197341) and [this library](https://github.com/eligrey/FileSaver.js)). The FileSystem API as commented by @MarcoAcierno lets you create a ***virtual*** local file system inside the given browser, it will not give you access to the user's file system. – Fabrício Matté May 07 '14 at 11:52
  • The question is, what do you want to save? Why do you need to write into a text file?? – Mohammed Joraid May 07 '14 at 11:54
  • after i doing a split, i got the array and each of the array item i want to save into a txt file – Attila Naghi May 07 '14 at 11:57
  • who/what provided the array input? do you use server or your app run locally only? – Mohammed Joraid May 07 '14 at 12:02
  • @RokoC.Buljan The file system api provides a sandbox to work within so that files can be created. https://developer.mozilla.org/en-US/docs/Web/API/LocalFileSystem – CodingIntrigue May 07 '14 at 12:03
  • @RGraham Great let's inject some viruses HOHOHO!!!!!!! – Roko C. Buljan May 07 '14 at 13:20

1 Answers1

0

The question is, what do you want to save and why do you need to write into a text file??

It's a security breach to allow browsers to write into user's machines. Browser's script should not access and modify local file directory that belongs to the user. If so, viruses and other malicious codes will spread.

So if you want to save some data, you have two options:

Option 1: Save it to your server using server side script e.g. PHP. You can create as many files as you want, rename them, save them to db and edit them, etc. If you worry about redirecting the user (the user needs to submit a form). You can use it using AJAX where you send a request at the background to save the user's input and have php saving the file for you.

Sample code to write to file using php:

$f = fopen('/path/to/the/file/you/want/to/write/to', 'a');    
fwrite($f, '<<your string>>');
fclose($f);

Option 2:

Save it into the browser's storage. Fast and works offline. There are several types of browser storage such as localStorage they are all built in and can be used directly.

Storage objects are a recent addition to the standard. As such they may not be present in all browsers.........The maximum size of data that can be saved is severely restricted by the use of cookies.

Code sample:

  function storeMyContact(id) {
    var fullname    = document.getElementById('fullname').innerHTML;
    var phone       = document.getElementById('phone').innerHTML;
    var email       = document.getElementById('email').innerHTML;
    var comments            = "this user has saved his info"
    localStorage.setItem('mcFull',fullname);
    localStorage.setItem('mcPhone',phone);
    localStorage.setItem('mcEmail',email);
    localStorage.setItem('comments',comments);

  }

On the other hand, localStorage might not be enough, therefore, external libraries come to hand which actually utilize the browsers built in storage and make the db works cross browsers.

1- SQL like DB sequelsphere (looks like suitable for heavy lifting!)

Code sample for query that will run directly from the browser using MYSQL like INSERT and SELECT:

SELECT empl_id, name, age 
  FROM empl 
 WHERE age < 30 

2- JSON like DB taffydb (looks like suitable for every day activity!)

// Create DB and fill it with records
var friends = TAFFY([
    {"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active"},
    {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active"},
    {"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active"},
    {"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active"}   
]);

   // Find all the friends in Seattle
   friends({city:"Seattle, WA"});

3- jstorage is a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in desktop (yes - even Internet Explorer 6) and in mobile.

If you would like to have more options ->(client-side-browser-database)

Community
  • 1
  • 1
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38