6

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);

OR

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

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

Josh Mc
  • 9,911
  • 8
  • 53
  • 66
  • possible duplicate of [Is there any way to specify a suggested filename when using data: URI?](http://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-when-using-data-uri) – Andy E May 30 '11 at 12:09
  • This does appear to be a duplicate of the suggested link, not sure why I didn't find it when I searched. People wanting an answer to this question should follow the link. – Josh Mc May 01 '14 at 21:30

2 Answers2

4

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

Alexander
  • 883
  • 7
  • 17
  • 1
    IE10 features msSaveOrOpenBlob and msSaveBlob. Theese are two methods available in IE10 which let sites ask the user to save a blob to their computer. More at http://blogs.msdn.com/b/ie/archive/2012/01/27/creating-files-through-blobbuilder.aspx – Alexander Apr 24 '12 at 13:36
0

I think you should check: jQuery Table to CSV export

Community
  • 1
  • 1
XzKto
  • 2,472
  • 18
  • 18
  • These solutions look like they are dependent on server-side scripting, I am looking to stick to client-side. Thanks for suggestion anyway. – Josh Mc May 31 '11 at 04:03