Last active
March 10, 2019 06:08
-
-
Save konojunya/0a8a77603044dbed4aad76e56e775053 to your computer and use it in GitHub Desktop.
file download javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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