0

I've been looking around, and I find something that for some reason works for some links, but when I try to download my image it just opens the page with the image

<a href="https://i.imgur.com/KBUpwNd.jpg" download="V2Map">Download</a>

I expect this to download the imgur image I linked, but it just opens the link instead, any ideas how to fix this?

3 Answers3

0

The download attribute only works for same origin URLS. You could either write a server side script.

OR Take a look at this discussion for a quick workaround.

Simply insert your URL into the downloadResource() like so: downloadResource('https://i.imgur.com/KBUpwNd.jpg'); Wrap in script tags and run on your browser.

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
0

I've just made a quick test and it seems like that origin domain matters in this case.

The download attribute works fine if the image is on the same domain. Otherwise it opens the link.

  • This attribute only works for same-origin URLs.
  • Although HTTP(s) URLs need to be in the same-origin, blob: URLs and data: URLs are allowed so that content generated by JavaScript, such as pictures created in an image-editor Web app, can be downloaded.
  • If the HTTP header Content-Disposition: gives a different filename than this attribute, the HTTP header takes priority over this attribute.
  • If Content-Disposition: is set to inline, Firefox prioritizes Content-Disposition, like the filename case, while Chrome prioritizes the download attribute.
Ibu
  • 42,752
  • 13
  • 76
  • 103
0

According to the MDN description of the download attribute:

This attribute only works for same-origin URLs.

So it won't work with a URL that points to a different domain than your own, such as i.imgur.com.

You can use a proxy script on your own server, something like:

<a href="/image_download.php?url=https://i.imgur.com/KBUpwNd.jpg" download="V2Map">Download</a>

Then write the image_download.php script that does:

readfile($_GET['url']);

You should of course have validation checks in the script so that it doesn't get abused as a general-purpose proxy by third parties. Google "php proxy" and you'll find some pre-written scripts.

Barmar
  • 741,623
  • 53
  • 500
  • 612