chsdk / js-file-download

This code shoes the best solution to open download file pop-up in a web page using JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

js-file-download

This code shoes the best solution to open download file pop-up in a web page using JavaScript.

If you need to download a file to the client browser with javascript, you just need to call the downloadFile(filePath) function with the filePath url as an input prameter, it will load the file and download it in the browser :

downloadFile("http://www.adobe.com/content/dam/Adobe/en/accessibility/pdfs/accessing-pdf-sr.pdf");

I shared this code when I was trying to provide the best solution as an answer to this question on stackoverflow. You can see it working in this live DEMO.

Notes:

  • You have to use the link.download attribute so the browser doesn't open the file in a new tab and fires the download pop-up.
  • This was tested with several file extensions (docx, xlsx, png, pdf, ...) and works in almost all browsers Chrome, Firefox,, Opera, IE and Safari.

About

This code shoes the best solution to open download file pop-up in a web page using JavaScript.

License:MIT License


Languages

Language:JavaScript 100.0%