How to generate and prompt to save a file from content in the client browser? [duplicate] How to generate and prompt to save a file from content in the client browser? [duplicate] javascript javascript

How to generate and prompt to save a file from content in the client browser? [duplicate]


This "FileSaver" library may help. If you want it to be reasonably cross-browser, you'll also need this to implement the W3C Blob API in places it's not already implemented. Both respect namespaces, and are completely framework agnostic, so don't worry about naming issues.

Once you've got those included, and as long as you're only saving text files, you should be able to

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

Note that the first argument to new Blob has to be a list of strings, and that you're expected to specify the filename. As in, the user will see this file being downloaded locally, but won't be able to name it themselves. Hopefully they're using a browser that handles local filename collisions...


This is my code:

<a id='tfa_src_data'>Export</a>document.getElementById('tfa_src_data').onclick = function() {                                          var csv = JSON.stringify(localStorage['savedCoords']);                        var csvData = 'data:application/csv;charset=utf-8,'                                        + encodeURIComponent(csv);                        this.href = csvData;                        this.target = '_blank';                        this.download = 'filename.txt';                    };

You can use various data types.


Depending on what you are trying to do exactly, the HTML5 local storage concept might help you.

So what is HTML5 Storage? Simply put, it’s a way for web pages to store named key/value pairs locally, within the client web browser. Like cookies, this data persists even after you navigate away from the web site, close your browser tab, exit your browser, or what have you. Unlike cookies, this data is never transmitted to the remote web server (unless you go out of your way to send it manually). http://diveintohtml5.info/storage.html

There's also the Filesystem API (so far only implemented in Chrome AFAIK)http://www.html5rocks.com/en/tutorials/file/filesystem/