Skip to content

Instantly share code, notes, and snippets.

@konojunya
Last active March 10, 2019 06:08
Show Gist options
  • Save konojunya/0a8a77603044dbed4aad76e56e775053 to your computer and use it in GitHub Desktop.
Save konojunya/0a8a77603044dbed4aad76e56e775053 to your computer and use it in GitHub Desktop.
file download javascript
function download(url, filename){
var xhr = new XMLHttpRequest(),
a = document.createElement('a'), file;
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function () {
file = new Blob([this.response], { type : 'application/octet-stream' });
a.href = window.URL.createObjectURL(file);
a.download = filename;
a.click();
};
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment