1

My code:

<div>
    <a href="/images/test-201.gif" class="download">Download</a>
</div>

I need to do is when I click on the Download. It should open a new window like save as for this image. I need to do this using HTML or javascript.

Jeroen
  • 1,168
  • 1
  • 12
  • 24

4 Answers4

3

In short: you cannot achieve this using javascript and html only.

Without a server-side language (like php) you won't be able to force a file download. The server needs to send the image to the client along with the right response headers

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

Short answer: It's not posible. You have to POST it to the backend (server) and response it back from server using the header Content-disposition: attachment.

Also this wont work as an AJAX response so you have to do something like

document.location = 'download_image.php?file=test-201.gif';

which response the download with the correct header as mentioned above.

As far as I know this is the only cross-browser solution to trigger downloads via javascript.

GodLesZ
  • 899
  • 5
  • 6
0

You can do this using an attribute known as download. Just add the attribute download="test image" to your anchor tag. "test image" can be any name you want to give to the image. Here is the syntax :

<a href="/images/test-201.gif" class="download" download="test image">Download</a>
Swaprks
  • 1,553
  • 11
  • 16
0

You cannot achieve this using javascript only.

Without a server-side language (like php) you won't be able to do this. The server needs to send the image to the client.

Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28