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
I found good answers here, but also found a simpler way.
The button to create the blob and the download link can be combined in one link, as the link element can have an onclick attribute. (The reverse seems not possible, adding a href to a button does not work.)
You can style the link as a button using
bootstrap
, which is still pure javascript, except for styling.Combining the button and the download link also reduces code, as fewer of those ugly
getElementById
calls are needed.This example needs only one button click to create the text-blob and download it:
Some suggestions for this -
If you are talking about browser javascript, you can not write data directly to local file for security reason. HTML 5 new API can only allow you to read files.
But if you want to write data, and enable user to download as a file to local. the following code works:
to use it:
download('the content of the file', 'filename.txt', 'text/plain');
Use the code by the user @useless-code above (https://stackoverflow.com/a/21016088/327386) to generate the file. If you want to download the file automatically, pass the
textFile
that was just generated to this function:Above answer is useful but, I found code which helps you to download text file directly on button click. In this code you can also change
filename
as you wish. It's pure javascript function with HTML5. Works for me!You can create files in browser using
Blob
andURL.createObjectURL
. All recent browsers support this.You can not directly save the file you create, since that would cause massive security problems, but you can provide it as a download link for the user. You can suggest a file name via the
download
attribute of the link, in browsers that support the download attribute. As with any other download, the user downloading the file will have the final say on the file name though.Here's an example that uses this technique to save arbitrary text from a
textarea
.If you want to immediately initiate the download instead of requiring the user to click on a link, you can use mouse events to simulate a mouse click on the link as Lifecube's answer did. I've created an updated example that uses this technique.