File download - JavaScript

Open download file pop-up in a web page using JavaScript.

by Sadek Chaabene

JavaScript

function downloadFile(filePath) {
  var link = document.createElement('a');
  link.href = filePath;
  link.onclick = function() {
    document.body.innerText = "The file is being downloaded ...";
    setTimeout(function() {
      document.body.innerText = "";
    }, 2000);
  }
  link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
  link.click();
}

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