I want to create a one-click download link for images. I get inconsistent results with the html5 download attribute.
W3 schools has a working example triggering a download: https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download
When I change the image urls to anything else it does not trigger a file download.
For example this code doesn't trigger a download:
<a href="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg" download>
Can anyone explain why?
In the w3schools example that you have attached, the link text which you should click on to start download is an image. They have put an image inside of an anchor tag (
<a></a>) and then have given URL to the href attr of the anchor. Then if you change the href, the url will change without changing the image and you can start downloading by clicking on the image.in the code that you have attached to your question (Which is working and download an image as a file if you add the
</a>to the end of it.), you've given the URL of an image as href attr. So if you change the href, different file would be downloaded.if you want to click on your image to start the download, you should put an image inside of the anchor and then gave the href attr, your file URL.