pareshm pareshm - 5 months ago 27
HTML Question

Is it possible to write data to file using only JavaScript?

I want to Write Data to existing file using JavaScript.
I don't want to print it on console.
I want to Actually Write data to

abc.txt
.
I Read many answered question but every where they are printing on console.
at some place they have given code but its not working.
So please can any one help me How to actually write data to File.

I referred the code but its not working:
its giving error:

Uncaught TypeError: Illegal constructor


on chrome and

SecurityError: The operation is insecure.


on Mozilla

var f = "sometextfile.txt";

writeTextFile(f, "Spoon")
writeTextFile(f, "Cheese monkey")
writeTextFile(f, "Onion")

function writeTextFile(afilename, output)
{
var txtFile =new File(afilename);
txtFile.writeln(output);
txtFile.close();
}


So can we actually write data to file using only Javascript or NOT?
Please Help Me
Thanks in advance

Answer Source

Some suggestions for this -

  1. If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
  2. If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
  3. To store some information on the client side that is considerably small, you can go for cookies.
  4. Using the HTML5 API for Local Storage.