How can i generate a file and offer it for download in plain javascript?

I would like to generate a text file in the javascript dynamicly, then offer this for download. Currently I can get this working to a degree with either of the following solutions:

content = "abc123";
document.location = "data:text/octet-stream," + encodeURIComponent(content);


content = "abc123";    
var bb = new BlobBuilder();
var blob = bb.getBlob();
blob = blob.slice(0, blob.size, 'text/octet-stream');
var fr = new FileReader(); 
fr.onload = function() {document.location = this.result;}

However, Both of these solutions, when the download box appears, will only offer a default filename of 'download' in the save as dialogue.

My question is basically, how can I change this to a specific filename for example 'readme.txt' or 'scene.obj'

Also note the data type was previously 'text/plain' however if this is used, the document switches to the new text document instead of offering it for download (as text/octet-stream seems to do).

I do not want a flash solution, javascript/html5 only suggestions please.

Cheers, Josh



For that, you will have to use FileSaver from FileAPI: Writer specification. For now, it's only a draft, and according to mailing list answer it isn't yet implemented in browsers.

You can watch for example on a chromium issue to get up-to-date information about the implementation progress

UPD 02.08.2013: I have since found a project that provides FileSaver interface using neat tricks


I think you should check: jQuery Table to CSV export


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.