Force a browser to save file as after clicking link

JavascriptJqueryHtml

Javascript Problem Overview


> Possible Duplicate:
> Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?

Is there a way to force a browser with a JavaScript routine to save a file as ("save as") after clicking a link? I need for images to be downloaded directly by default and not rendered by the browser.

Javascript Solutions


Solution 1 - Javascript

You could use the HTML5 download attribute like so:

<a href="path/to/file" download>Click here to download</a>

This opens a "save as" dialog regardless of file type without taking you away from the page.

Solution 2 - Javascript

If you control the server, then you should set it up to send a Content-Disposition: attachment header for the files you wish to be downloaded (e.g. you can do this in an .htaccess file to make all .jpegs in a particular directory).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackson HenleyView Question on Stackoverflow
Solution 1 - JavascriptAustinView Answer on Stackoverflow
Solution 2 - JavascriptM SomervilleView Answer on Stackoverflow