Code Premix

How to download a file using JavaScript

📅August 17, 2022

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.

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

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..!!