Created
April 12, 2017 07:47
-
-
Save yanfishel/e861879d6c49da54922a431b42ac671f to your computer and use it in GitHub Desktop.
JS File Download
This file contains hidden or 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
| 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