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.
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.
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
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.
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>
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.