Skip to content

Instantly share code, notes, and snippets.

@yanfishel
Created April 12, 2017 07:47
Show Gist options
  • Select an option

  • Save yanfishel/e861879d6c49da54922a431b42ac671f to your computer and use it in GitHub Desktop.

Select an option

Save yanfishel/e861879d6c49da54922a431b42ac671f to your computer and use it in GitHub Desktop.
JS File Download
var saveFile = function (url, filename) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function () {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment