Download a file using JavaScript
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.
Checkout more articles on JavaScript
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.
1 2 3 4 5 6 7 8 | 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..!! 🙂