Created
November 16, 2016 03:07
-
-
Save antfu/d957207c757cd074a14ccb0c04c30807 to your computer and use it in GitHub Desktop.
[JS] Download file
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(data, filename, type) { | |
var a = document.createElement('a') | |
var file = new Blob([data], { type: type }) | |
if (window.navigator.msSaveOrOpenBlob) // IE10+ | |
window.navigator.msSaveOrOpenBlob(file, filename) | |
else { // Others | |
var url = URL.createObjectURL(file) | |
a.href = url | |
a.download = filename | |
document.body.appendChild(a) | |
a.click(); | |
setTimeout(function () { | |
document.body.removeChild(a); | |
window.URL.revokeObjectURL(url); | |
}, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment