Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force browser to download an image on click of a link using jquery

I would like to start download of an image on click of a link.

<a id="downloadImage" href="imagepath">Click here to download</a>

I know we can use download attribute of HTML5 (Force a browser to save file as after clicking link) but I would not like to use it as it will not be working in older versions of browsers. I did tried the method here: Download File Using Javascript/jQuery but it opens the image in the iframe .

Can anybody help me to force a browser to download an image onclick of a link using jquery?

like image 330
Maninder Avatar asked Oct 29 '13 10:10

Maninder


2 Answers

As far as I know there is no client-side cross-browser solution for this issue (doesn't matter using jQuery or any other UI toolkit). What you need to do in order to trigger browser to download a file is to add some HTTP headers to the server response:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=image.jpg

This post may also be helpful for you.

like image 200
Vadim Avatar answered Sep 22 '22 04:09

Vadim


You can use the download attribute, while not fully supported across browsers, you can use modernizr to support/fallback for unsupported browsers.

For supported browsers, check http://caniuse.com/#feat=download

<a href="/path/to/image.jpg" title="ImageName" download="ImageName" >
    <img src="/path/to/image.jpg" alt="ImageName">
</a>
like image 28
Vin Lim Avatar answered Sep 21 '22 04:09

Vin Lim