javascript

How to Download File in Javascript


In this article, you will learn how to download a file in Javascript.

Let’s say you want to download Codesource.io’s logo.

Download File

In order to download a file, you can use the HTML’s download attribute.

function download(fileUrl, fileName) {
  var a = document.createElement("a");
  a.href = fileUrl;
  a.setAttribute("download", fileName);
  a.click();
}

download("/static/ad920d8be55b3543cac62c795aa80df5/codesource.png", "Codesource_Logo.png");

Note: The HTML’s download attribute functions by downloading the supplied file URL upon clicking.


Share on social media

//