I was wondering whether it is possible to force a browser (at least Chrome) to download a data:text/plain
URL.
Chrome does download binary URLs (e.g. data:application/zip;base64,...
), but it does not download files that can be viewed inside the browser (such as text files).
What I already tried with no luck so far is this:
data:text/plain;content-disposition=attachment;filename=test.txt;...
But it seems like I cannot add headers like this.
Is there any way to make Chrome download a data:text/plain,...
URL?
As of now, it has been made possible to use
<a download>
in Chrome. UsingdispatchEvent
, you can download any string as file (even with a custom filename) whenever you want. Here's a utility function to use it:Usage:
It uses jQuery and the
webkit
prefix, but both can be avoided.What I did was sending the data to a server, which sends them back with the following HTTP header:
I don't like this, but it works rather well.
This works as hell ...
It uses HTML5 attribute
download="filename.ext"
. (no JS needed:)More about: http://www.w3.org/TR/html/links.html#downloading-resources
Browser support can be checked at http://caniuse.com/download
(As for now, 2013, no IE nor Safari support)
I think, you can make a fallback for not-supporting browsers: use JS to change value of
href="..."
to the URL of your server script (which will return the file contents with appropriate HTTP headerContent-disposition: attachment;filename=filename.txt
).