admin管理员组

文章数量:1336645

Is it possible, in Javascript, to prompt user for downloading a file that isn't actually on the server, but has contents of a script variable, instead?

Something in spirit with:

var contents = "Foo bar";
invoke_download_dialog(contents, "text/plain");

Cheers,

MH

Is it possible, in Javascript, to prompt user for downloading a file that isn't actually on the server, but has contents of a script variable, instead?

Something in spirit with:

var contents = "Foo bar";
invoke_download_dialog(contents, "text/plain");

Cheers,

MH

Share Improve this question asked Jul 13, 2009 at 16:20 Mike HordeckiMike Hordecki 97.2k3 gold badges29 silver badges28 bronze badges 1
  • 2 This sounds like it would be a serious security flaw – John Rasch Commented Jul 13, 2009 at 16:22
Add a ment  | 

6 Answers 6

Reset to default 3

javascript: URIs should work for this - indeed, this is exactly what they're meant for. However, IE doesn't honour the type attribute, and in Safari this technique has no effect at all.

data: URIs work in Firefox (3.0.11) and Safari (4.0) (and probably other pliant browsers), but I can't get this approach to work in IE (8.0). (All tested in Windows)

<a href="data:text/plain,The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog.">Data URI</a>

This isn't a JS solution in itself, but JS can be used to set the href dynamically. Use the escape function to turn raw text/data into URI-encoded form.

Combining this with detecting IE and using the IE-specific solution already linked to might do what you want....

I shall add that you can't force it to trigger a download dialog (that's beyond the scope of both HTML and JS), but you can persuade it to do so by setting application/octet-stream as the type. Trouble is that the user'll then have to add the right filename extension manually.

A possible option would be to use JavaScript to generate a link with a href using the data: URL scheme, tho' this might require some fancy coding to pull off properly.

See the accepted answer to my question here. This is only possible in IE browsers.

document.execCommand('SaveAs',true,'file.xml')

not specifically via javascript, but you could post the variable's value to a server-side page that would force the user to download the contents as text.

Keep in mind, if any cross site scripting vulnerability exists on the page, the variable could be overwritten with malicious data. Although the resulting file's content type would still need to be by-passed for an executable, there's still risk.

I think that a safer way to do this would be to use Joel's answer and to perform validation on the posted data before sending it to the user.

Check out jsPDF; it allows downloading (or embedded viewing) of PDF files that are entirely generated inside the browser. They claim: "Client-side demo works best in Safari or iPhone Safari. Also works Firefox 3 on Windows and Opera. IE support on the way." So, they seem to have a handle on how to invoke downloads in major browsers.

本文标签: javascriptDownloading a variableStack Overflow