admin管理员组

文章数量:1335871

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

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

Share Improve this question asked May 30, 2011 at 11:01 Josh McJosh Mc 10.3k8 gold badges56 silver badges68 bronze badges 2
  • possible duplicate of Is there any way to specify a suggested filename when using data: URI? – Andy E Commented May 30, 2011 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 Commented May 1, 2014 at 21:30
Add a ment  | 

2 Answers 2

Reset to default 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

I think you should check: jQuery Table to CSV export

本文标签: htmlHow can i generate a file and offer it for download in plain javascriptStack Overflow