Categories
JavaScript Answers

How to download image with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to download image with JavaScript?”

Leave a Reply

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