sugar
sugar

Reputation: 251

is there a way to create file and write data to it using javascript/jquery

Is there a way to create and write data to a file using javascript/jquery. I referred this link https://gist.github.com/Arahnoid/9925725#file-read-write-file-js . But it gives me error for tis line

var file = new File("E:/abc/sample.txt); 
 error: NS_ERROR_FAILURE: in firefox

Upvotes: 0

Views: 4708

Answers (2)

Nick Hermans
Nick Hermans

Reputation: 86

You can't write files in javascript using your browser without asking the user for permission first.

you can prompt the user to save a file using the following liberary:

https://github.com/eligrey/FileSaver.js

POC code:

<script src="FileSaver.js"></script>
<script>
    var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
    saveAs(blob, "hello world.txt");
</script>

This will prompt the user to save "Hello world!" to the file "Hello, world.txt" file.

Upvotes: 1

Parag Bhayani
Parag Bhayani

Reputation: 3330

Javascript is client side language which runs on server, and it doesn't have access to client file system, I don't know what your exact requirement is but this might help you

http://tutorialzine.com/2011/05/generating-files-javascript-php/ https://github.com/eligrey/FileSaver.js

Would you let me know what is your exact requirement then I might help you

Upvotes: 0

Related Questions