I want to make a <a href
link which, when clicked, forces your browser to open a "Save As" dialogue. The use of the HTML5 download
attribute should have this behavior on all modern browsers.
When the target is an externally hosted image file, this does not work. The <a href
link will navigate to the image instead of downloading it. (Tested using images hosted on Imgur and Tumblr)
<a href="https://i.stack.imgur.com/440u9.png" download>
<img src="https://i.stack.imgur.com/440u9.png" width="200"/>
</a>
Similar HTML code does work if the image is hosted locally on your server, for example
<a href="440u9.png" download>
<img src="440u9.png" width="200"/>
</a>
Another example - this will work in the W3Schools Tryit Editor, but will not work if you copy the HTML to another sandbox like JSFiddle
<a href="https://www.w3schools.com/images/myw3schoolsimage.jpg" download>
<img src="https://www.w3schools.com/images/myw3schoolsimage.jpg" width="104" height="142">
</a>
Possible causes explored:
- Bad image URL: no, the URL is good, you can open the URL as a plain image in your browser
- Links with https: can't be used this way due to security: no, a URL with
https:
is good if the URL points to the same domain, tested in W3Schools Tryit Editor - Server-side redirect: an
http://...
link can be redirected tohttps://...
so maybe that could be a cause of failure, but thehttps://...
link also does not work - Suppression of download attribute on server by specific hosts? is that even possible? the download attribute should control only client-side browser action surely?
Any workarounds? I have tried Javascript download methods discussed here and here but these all eventually involved a browser action similar to clicking on an href
link.
Excellent in-depth discussion of download links including application/force-download
setting in PHP on the server
Related StackOverflow question: Force external download url (answer seems to be incorrect)