If you also want to give a suggested name to the file (instead of the default 'download') you can use the following in Chrome, Firefox and some IE versions:

function downloadURI(uri, name) {
  var link = document.createElement("a");
  link.download = name;
  link.href = uri;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  delete link;
}

And the following example shows it's use:

downloadURI("data:text/html,HelloWorld!", "helloWorld.txt");
Answer from owencm on Stack Overflow
🌐
GitHub
gist.github.com › gkhays › fa9d112a3f9ee61c6005136ebda2a6fd
Download a file from a URL using Node.js · GitHub
https : http; const request = client.get(url, function(response) { var len = parseInt(response.headers['content-length'], 10); var cur = 0; var total = len / 1048576; //1048576 - bytes in 1 Megabyte response.on('data', function(chunk) { cur += chunk.length; showProgress(file, cur, len, total); }); response.on('end', function() { console.log("Download complete"); }); response.pipe(localFile); }); } function showProgress(file, cur, len, total) { console.log("Downloading " + file + " - " + (100.0 * cur / len).toFixed(2) + "% (" + (cur / 1048576).toFixed(2) + " MB) of total size: " + total.toFixed
Discussions

Download File with the help of JavaScript - JavaScript - SitePoint Forums | Web Development & Design Community
I have created a function to download the file, but the issue is it opens the file on the same tab, I a trying to force download that file. I read other threads but didn’t get the answer as I was expected and helpful. Please help me create this function that downloads the file. More on sitepoint.com
🌐 sitepoint.com
0
August 7, 2022
JavaScript Download?
Javascript is already build in in your browser. What you can Download is NodeJS, but you won't need that yet in the beginning. https://jgthms.com/javascript-in-14-minutes/ More on reddit.com
🌐 r/learnprogramming
3
1
January 16, 2023
What's the difference between .js and .js.download files?
usually .download or .part are incomplete files More on reddit.com
🌐 r/techsupport
8
2
February 24, 2024
How to start a client-side download with JavaScript
You don’t need to add the to the DOM More on reddit.com
🌐 r/learnjavascript
20
220
January 19, 2023
🌐
Kodeclik
kodeclik.com › javascript-download-file-from-url
How to download a file from a URL using Javascript
September 1, 2025 - We create a URL for the file data using the window.URL.createObjectURL() method and create a new element with the href attribute set to the URL of the file and the download attribute set to the desired filename.
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-download-a-file-from-a-url-in-javascript
How to Download a File from a URL in JavaScript | Atomized Objects
In this article, we will explore different techniques and methods to download files from a URL using JavaScript. We will cover the use of anchor tags, the fetch() API, and the XMLHttpRequest object.
🌐
CoreUI
coreui.io › answers › how-to-download-a-file-in-javascript
How to download a file in JavaScript · CoreUI
December 2, 2025 - Learn how to programmatically download files in JavaScript using blob URLs and anchor elements for file download functionality.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › download-any-file-from-url-with-vanilla-javascript
Download Any File From URL with Vanilla JavaScript - GeeksforGeeks
Downloading files from a URL using vanilla JavaScript involves creating a link element, setting its href attribute to the file URL, and programmatically triggering a click event. This method allows users to download files without relying on ...
Published   August 5, 2025
🌐
CodingNepal
codingnepalweb.com › home › javascript projects › download any file from url with vanilla javascript
Download Any File From URL with Vanilla JavaScript
May 24, 2022 - If you liked this file downloader and want to get source codes or files, you can easily get them from the bottom of this page. But, before you download the files, let’s talk about the codes and concepts behind creating this file downloader with JavaScript. At first, I got the user entered file URL, and using fetch() API, I fetched the file.
Find elsewhere
🌐
LogRocket
blog.logrocket.com › home › programmatically downloading files in the browser
Programmatically downloading files in the browser - LogRocket Blog
August 28, 2024 - Master file downloads in HTML using JavaScript, and explore the role of blobs, object URLs, and anchor elements.
🌐
Attacomsian
attacomsian.com › blog › javascript-download-file
How to download a file in JavaScript
September 10, 2022 - fetch('https://reqres.in/api/users') .then(res => res.json()) .then(json => { // Convert JSON to string const data = JSON.stringify(json); // Create a Blob object const blob = new Blob([data], { type: 'application/json' }); // Create an object URL const url = URL.createObjectURL(blob); // Download file download(url, 'users.json'); // Release the object URL URL.revokeObjectURL(url); }) .catch(err => console.error(err)); Introducing StartupBase 2 🚀 · Get the length of a Map in JavaScript · Delete an element from a Map in JavaScript ·
🌐
jamieonkeys
jamieonkeys.dev › posts › chatgpt-file-download-browser
Writing some JavaScript to download files from the web, with help from an AI chatbot - jamieonkeys
May 1, 2023 - To fetch and download a PDF using JavaScript, you can use the fetch function to get the PDF data as a Blob and then create a download link using that Blob. Here’s an example function that takes a URL and a filename as parameters:
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript download
How to Download a File Using JavaScript | Delft Stack
February 2, 2024 - Set href as the URL created in the first step and download attribute as the downloaded file’s name. Attach this link to the document and simulate a click using the .click() method. Remove this link from the document. <!DOCTYPE html> <html> <head> <title>How to download files using JavaScript</title> </head> <body> <button onclick="download()"> Download Image </button> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"> </script> <script> function download() { axios({ url: 'https://source.unsplash.com/random/500x500', method: 'GET', responseType: 'blob' }) .then((response) => { const url = window.URL .createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'image.jpg'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }) } </script> </body> </html>
🌐
CodePel
codepel.com › home › vanilla javascript › javascript download file from url programmatically
JavaScript Download File From URL Programmatically — CodePel
February 16, 2025 - Here is a free JavaScript code snippet to download file from URL. You can view live demo and download the source code.
Address   Rafi Qamar Road, Al-Majeed Peradise Al Majeed Peradise, 62300, Bahawalpur
🌐
Flexiple
flexiple.com › javascript › download-flle-using-javascript
How to Download a File Using JavaScript - Flexiple
JavaScript provides the ability to trigger file downloads from a web page using the `Blob` object and the `URL.createObjectURL` method to download a file using JavaScript, developers often employ specific techniques. JavaScript creates a `Blob` object, encapsulating the file data.
🌐
Javascripts
javascripts.com › download-files-from-urls-with-javascript
How to Download Files from URLs with JavaScript
June 23, 2023 - We use JavaScript’s Fetch API to retrieve the file from the specified URL. Then, we convert the response into a blob object. Using the FileSaver.js library, we trigger the file download on the client side with the specified filename and extension.
🌐
SitePoint
sitepoint.com › javascript
Download File with the help of JavaScript - JavaScript - SitePoint Forums | Web Development & Design Community
August 7, 2022 - I have created a function to download the file, but the issue is it opens the file on the same tab, I a trying to force download that file. I read other threads but didn’t get the answer as I was expected and helpful. P…
🌐
Transcoding
transcoding.org › home › javascript download file from url: a deep dive with code samples
JavaScript Download File from URL: A Deep Dive with Code Samples — Transcoding
February 3, 2024 - One such gem is FileSaver.js, which provides a simple way to save files on the client-side. ... import { saveAs } from 'file-saver'; const DownloadButton = ({ url, fileName }) => { const handleDownload = async () => { try { const response = await fetch(url); const blob = await response.blob(); saveAs(blob, fileName); } catch (e) { console.log('Whoops, something went wrong with the download:', e); } }; return ( <button onClick={handleDownload}>Download</button> ); }; // Usage // <DownloadButton url="https://example.com/file.pdf" fileName="cool-file.pdf" />
🌐
CodeSignal
codesignal.com › learn › courses › efficient-api-interactions-with-javascript › lessons › downloading-files-from-an-api-using-javascript
Downloading Files from an API Using JavaScript
GET requests are fundamental for retrieving files from an API. When you send a GET request using fetch, your client communicates with the server at a specified URL, asking it to provide the file. The server responds with the file data, if available and permissible, along with an HTTP status code (like 200 OK). Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes.
🌐
DEV Community
dev.to › incoderweb › how-to-create-a-file-downloader-via-url-using-pure-javascript-obp
How to create a file downloader via url using pure javascript - DEV Community
May 26, 2022 - let form = document.querySelector('#fileDownloader') downloadBtn = form.querySelector('button') form.addEventListener('submit', e => { e.preventDefault() let inputURL = e.target.querySelector('#fileURL').value.trim() downloadBtn.innerText = 'Downloading File Please Wait....' downloadBtn.setAttribute('disabled', 'disabled') getFile(inputURL) }) const getFile = (url) => { fetch(url).then(response => response.blob()).then(file => { let tempLink = URL.createObjectURL(file) let anchorTag = document.createElement('a') anchorTag.href = tempLink anchorTag.download = url.replace(/^.*[\\\/]/, '') docume