-3

I'm trying to create an Onclick image download link, when a user clicks on the link/button he should get an option to download

here is my code below

<form>
<input type="button" value="download" onClick="window.location.href='http://myimagelink.com'"   >
</form>

Why it is not working?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Friend
  • 1,326
  • 11
  • 38
  • 62

2 Answers2

1

That code won't work because you have added an s to the end of window.

You should be using a regular <a href=""> for this though.

To trigger a download instead of causing the image to be rendered in the browser window, you need to use the Content-Disposition HTTP header to mark it as an attachment.

e.g. with Apache configuration:

<IfModule mod_headers.c>
    <FilesMatch "\.jpeg$">
        Header set Content-Disposition "attachment"
    </FilesMatch>
</IfModule>

There is no sensible way to use JavaScript for this (although, I imagine you could use XMLHttpRequest to download the image, generate a data: scheme URI from that, and then set location.href to that URI while lying about the content type).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

this worked well for me ..

<a href="http://dummyimage.com/600x400/000/fff.png" download>Download this image</a>

Friend
  • 1,326
  • 11
  • 38
  • 62