• Post category:JavaScript
  • Reading time:1 mins read

Today we will show you how to download a file using JavaScript. Here we will give you the JavaScript method to download the file from the URL.

Let’s assume that we have an download URL and filename to download the file from the URL. So we will generate a tag and trigger the click event to download the file.

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

download("https://DOMAIN.COM/FILE_NAME.txt", "FILE_NAME.txt");
Note: The HTML’s download attribute is used to download a file when clicking on the link (instead of navigating to the file).

I hope you find this article helpful.
Thank you for reading. Happy Coding..!!

Leave a Reply