How to download image with JavaScript?

Sometimes, we want to download image with JavaScript.

In this article, we’ll look at how to download image with JavaScript.

How to download image with JavaScript?

To download image with JavaScript, we create a link.

For instance, we write

const a = document.createElement("a");
a.href = "img.png";
a.download = "output.png";
a.click();

to create a link with the createElement method.

Then we set its href property to the path to the file we want to download.

Next, we set the download property to the string with the filename of the downloaded file.

Then we call click to download the file.

Conclusion

To download image with JavaScript, we create a link.