Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

href image link download on click

I generate normal links like: <a href="/path/to/image"><img src="/path/to/image" /></a> in a web app.

When I click on the link, it displays the picture in a new page. If you want to save the picture, then you need to right click on it and select "save as"

I don't want this behaviour, I would like to have a download box popping out when I click on the link, is that possible just with html or javascript? How?

If not I guess I would have to write a download.php script and call it into the href with the file name as parameter...?

like image 972
Pierre Avatar asked Sep 28 '22 05:09

Pierre


2 Answers

<a download="custom-filename.jpg" href="/path/to/image" title="ImageName">
    <img alt="ImageName" src="/path/to/image">
</a>

It's not yet fully supported caniuse, but you can use with modernizr (under Non-core detects) to check the support of the browser.

like image 283
Francisco Costa Avatar answered Oct 17 '22 17:10

Francisco Costa


The easiest way of creating download link for image or html is setting download attribute, but this solution works in modern browsers only.

<a href="/path/to/image" download="myimage"><img src="/path/to/image" /></a>

"myimage" is a name of file to download. Extension will be added automatically Example here

like image 65
Aleksey Saatchi Avatar answered Oct 17 '22 17:10

Aleksey Saatchi