If for example you follow the link:
data:application/octet-stream;base64,SGVsbG8=
The browser will prompt you to download a file consisting of the data held as base64 in the hyperlink itself. Is there any way of suggesting a default name in the markup? If not, is there a JavaScript solution?
The following Javascript snippet works in Chrome by using the new 'download' attribute of links and simulating a click.
And the following example shows it's use:
Chrome makes this very simple these days:
Use the
download
attribute:Live example on html5-demos.appspot.com/....
Currently works on Chrome, Firefox, Edge, Opera, and desktop Safari but not iOS Safari or IE11.
you can add a download attribute to the anchor element.
sample:
Using service workers, this is finally possible in the truest sense.
<a href, <img src
, window.open( url ), absolutely anything that can be done with a "real" URL.The browser will now suggest myPrettyName.jpg even if the user opens the file in a new tab, and tries to save it there. It will be exactly as if the file had come from the server.
Here is a jQuery version based off of Holf's version and works with Chrome and Firefox whereas his version seems to only work with Chrome. It's a little strange to add something to the body to do this but if someone has a better option I'm all for it.