18

I want to be able to download a given file when pressing a button.The file will be provided via an API call.For now, I will have it in my local storage. So my folder is something like :

rootFolder
-JS file
-HTML file
-download file (`sample.csv`)

How can I create a download link? I have tried so far with : <a download="sample.csv"></a> I have also tried using an onclick event:

<a download="sample.csv" onclick="download()"></a>

function download|(){
   .....code that calls the `api`
}

I do not know how these 2 fit: the download API if there is one and the click event handler if you plan to do additional logic when downloading.

3

5 Answers 5

40

You can provide the link to this function to download the file :

function downloadURI(uri, name) 
{
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    link.click();
}
5
  • And if the file is in my local storage it works all the same ? Commented Feb 11, 2019 at 8:07
  • what exactly do you mean by local storage? aren't you using any local server ?
    – saibbyweb
    Commented Feb 11, 2019 at 8:09
  • You can simplify the function in one line as: const downloadFile = (url, filename) => Object.assign(document.createElement('a'), { href: url, download: filename }).click(); Commented May 15, 2022 at 13:02
  • ONLY suitable where the amount of data is less than the maximum allowable URI length (~2000) as explained by Paul Dixon's Jan 6, 2009 at 16:22
    – dank8
    Commented Oct 3, 2022 at 5:21
  • He probably meant: "What if the file is a blob" Commented Oct 30, 2022 at 13:41
20

since the answer from @saibbyweb does not work in all browsers as I write this, i recommend an other but similar solution, tested and working in latest (as of writing) Firefox, Chrome, Opera, Edge, Safari, mobile Safari, mobile Chrome:

function downloadUrl(url){
    window.open(url, '_self');
}

Needless to say you could also open links in new Tabs with _blank instead of _self, but you potentially startle Popup-Blockers by opening new tabs/windows with Javascript.

4
  • is there a way to set a file name this way? Commented Mar 30, 2022 at 14:05
  • @Zoid Since this directly opens an url, only the server can send approriate Headers to set a filename for the download.
    – mondjunge
    Commented Apr 1, 2022 at 10:11
  • 1
    Use the Content-Disposition header to set a file name (e.g., Content-Disposition: attachment; filename="sample.csv")
    – Sean
    Commented Apr 12, 2022 at 21:59
  • This might cause Session problems in mobile Safari Browsers, using window.open(url, '_parent'); resolved the issues.
    – mondjunge
    Commented Mar 21 at 16:09
0

You can do it via HTML <a href="/path/to/sample.csv"></a>, but if you have to do it in JS there is https://github.com/eligrey/FileSaver.js/ library.

-1

You can use this to download images and other files:

function downloadImg(url, name)  {
    let link = document.createElement('a');
    link.download = name;
    link.href = url;
    link.style.display = 'none';
    link.click();
}
1
  • This is just a copy of the accepted answer from 5+ years ago, with display: none added for some reason.
    – Chris
    Commented Apr 9 at 17:59
-3

You can create a Chrome's extension. You must have the download permission, in your "manifest.json". I put the code in the "browser_action" files, in the manifest.

chrome.downloads.download({url:"https://stackoverflow.com/questions/54626186/how-to-download-file-with-javascript"});

Not the answer you're looking for? Browse other questions tagged or ask your own question.