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
🌐
LogRocket
blog.logrocket.com › home › programmatically downloading files in the browser
Programmatically downloading files in the browser - LogRocket Blog
August 28, 2024 - Here is an example of a conventional HTML anchor element linking to a PDF document: In HTML 5, a download attribute was added to the anchor element. The download attribute is used to inform the browser to download the URL instead of navigating to it ...
Discussions

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
Can I download a file in JavaScript WITHOUT using blobs?
That’s why a manager is a manager and he should just stay in his lane. He clearly does not understand blob that well. More on reddit.com
🌐 r/webdev
30
103
March 4, 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
🌐
CoreUI
coreui.io › answers › how-to-download-a-file-in-javascript
How to download a file in JavaScript · CoreUI
December 2, 2025 - function downloadFile(data, filename, ... 'text/csv') This code creates a Blob object from the data, generates a temporary URL using createObjectURL, and creates an anchor element with the download attribute....
🌐
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
🌐
GitHub
gist.github.com › gkhays › fa9d112a3f9ee61c6005136ebda2a6fd
Download a file from a URL using Node.js · GitHub
import http from 'http' import https from 'https' import fs from 'fs' function getRemoteFile(file, url) { let localFile = fs.createWriteStream(file); const client = url.startsWith('https') ? 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
🌐
Kodeclik
kodeclik.com › javascript-download-file-from-url
How to download a file from a URL using Javascript
September 1, 2025 - There are at least 3 ways to download a file in Javascript. 1. Use the download attribute of the HTML “a” tag. 2. Create a download link programmatically. 3. Use the Fetch API.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Mozilla › Add-ons › WebExtensions › API › downloads › download
downloads.download() - Mozilla - MDN Web Docs
Otherwise, the promise will be rejected with an error message taken from downloads.InterruptReason. If you use URL.createObjectURL() to download data created in JavaScript and you want to revoke the object URL (with revokeObjectURL) later (as it is strongly recommended), you need to do that after the download has been completed. To do so, listen to the downloads.onChanged event. The following snippet attempts to download an example file, also specifying a filename and location to save it in, and uniquify as the value of the conflictAction option.
Find elsewhere
🌐
MUI Stack
muhimasri.com › blogs › how-to-save-files-in-javascript
How to Save and Download Files in JavaScript
December 12, 2021 - The easiest way to download and save a file programmatically in JavaScript is to dynamically create an anchor element with a download attribute and invoke the click event. Let’s create a function called saveFile that takes a URL and a file ...
🌐
Attacomsian
attacomsian.com › blog › javascript-download-file
How to download a file in JavaScript
September 10, 2022 - const download = (path, filename) => { // Create a new link const anchor = document.createElement('a'); anchor.href = path; anchor.download = filename; // Append to the DOM document.body.appendChild(anchor); // Trigger `click` event anchor.click(); // Remove element from DOM document.body.removeChild(anchor); }; // Example download download('atta-resume.pdf', 'resume.pdf'); ... Sometimes, you may want to save programmatically generated data as a file using JavaScript. That’s where blobs and object URLs are useful.
🌐
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 - Svelte is another rising star in the JavaScript framework sky, known for its radical approach to building user interfaces. When it comes to downloading files in Svelte, we can use a third-party library called downloadjs, which provides a simple and consistent API for file downloads. ... <script> import download from 'downloadjs'; async function downloadFile(url, fileName) { const response = await fetch(url); const blob = await response.blob(); download(blob, fileName); } </script> <button on:click="{() => downloadFile('https://example.com/file.pdf', 'cool-file.pdf')}"> Download </button>
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript download
How to Download a File Using JavaScript | Delft Stack
February 2, 2024 - Create an anchor tag <a> using the createElement property and assign download and href attributes to it. Set href as the URL created in the first step and download attribute as the downloaded file’s name.
🌐
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 - This tool (File Downloader) can download any file like image, video, pdf, svg, etc. Users have to paste a valid URL of the file and click the download button to download the file. Remember, the file should be publicly accessible to download.
🌐
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
This method provides more control over the download · process and allows for additional customization, such as setting custom headers or handling authentication. Another approach to download files is by using the XMLHttpRequest object, which provides a traditional way to make HTTP requests ...
🌐
Javascripts
javascripts.com › download-files-from-urls-with-javascript
How to Download Files from URLs with JavaScript
June 23, 2023 - We then use the ‘saveAs’ function from the FileSaver.js library. This function triggers a file download on the client side. We pass in the Blob we received and a string with the desired filename and extension. Finally, we call our ‘downloadFile’ function, passing in the URL of the file we want to download.
🌐
Flexiple
flexiple.com › javascript › download-flle-using-javascript
How to Download a File Using JavaScript - Flexiple
Employ the `XMLHttpRequest` or `fetch` method to handle the file retrieval process from the server. Ensure proper error handling is in place to manage any issues that arise during the download.
🌐
GitHub
gist.github.com › liabru › 11263260
Save a text file locally with a filename, by triggering a download in JavaScript · GitHub
– Anyone, please help me ....My Link ... function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', ...
🌐
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.
🌐
Plain English
plainenglish.io › home › blog › javascript › how to download files with javascript
How to Download Files with JavaScript
January 16, 2023 - If the download file url is from the same origin, it is recommended to use localtion.href or download attribute, otherwise we can use the fetch API to download file and turn it into blob type data for <a> element. I hope you found this article useful. You can learn more JavaScript at Certlibrary.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Mozilla › Add-ons › WebExtensions › Working_with_files
Work with files - Mozilla - MDN Web Docs
July 17, 2025 - The key method is downloads.download(), which in its simplest form accepts a URL and downloads the file from that URL to the user's default downloads folder: js · browser.downloads.download({ url: "https://example.org/image.png" }); You can let the user download to a location of their choice ...
🌐
Shinglyu
shinglyu.com › web › 2019 › 02 › 09 › js_download_as_file.html
Download JavaScript Data as Files on the Client Side | Shing's Blog
February 9, 2019 - Then, if we want to export the ... code: var text = 'Some data I want to export'; var data = new Blob([text], {type: 'text/plain'}); var url = window.URL.createObjectURL(data); document.getElementById('download_link').href = url; The magic ...