-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
  • 34,719
  • 24
  • 185
  • 251
Friend
  • 1,246
  • 10
  • 38
  • 60

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
  • 857,932
  • 118
  • 1,152
  • 1,264
-1

this worked well for me ..

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

Friend
  • 1,246
  • 10
  • 38
  • 60