Download images using HTML or JavaScript-ThrowExceptions

Exception or error:

I need to download images from the server. In the below HTML5 code ‘Download 1’ downloads the image successfully. But ‘Download 2’ is navigating to the image URL instead of downloading the image. The main difference between ‘Download 1’ and ‘Download 2’ is ‘Download 2′ has file extension as’.jpg’ in the file name. I want ‘Download 2’ to download the image.

I need to download images with the file extension. Please help to resolve the issue. Thanks in advance!

<a id="download_image" href="" download>Download 1</a>
<a id="download_image" href="" download>Download 2</a>
How to solve:

I think it works :


  <a id="download_image" href="" download="">Download 1</a>
  <a id="download_image" href="" download="http://serverpath/images/50.jpg">Download 2</a>


and checkout online demo if you like

and here is another lib do the same thing called file-writer:


<a id="download_image" href="" download="" download>Download 1</a>


It is working fine for me, maybe it will work for you if you try it like this by creating the link in javascript

var link = document.createElement('a');
link.textContent = 'download image';

link.addEventListener('click', function(ev) {
    link.href = ""; = "image.jpg";
}, false);




Try the following using Promise and async/await :

toDataURL(url) {
    return fetch(url).then((response) => {
            return response.blob();
        }).then(blob => {
            return URL.createObjectURL(blob);


async download() {
        const a = document.createElement("a");
        a.href = await toDataURL("http://serverpath/images/50.jpg"); = "";

Find documentation here:


Try this my code, using javascript and html
dont forget add

<script src=""></script>

Leave a Reply

Your email address will not be published. Required fields are marked *