Skip to content

Instantly share code, notes, and snippets.

@eeslom
Forked from antfu/download.js
Created September 14, 2024 21:00
Show Gist options
  • Select an option

  • Save eeslom/b0afd0a308a408526cc7211ca2c68c5d to your computer and use it in GitHub Desktop.

Select an option

Save eeslom/b0afd0a308a408526cc7211ca2c68c5d to your computer and use it in GitHub Desktop.
[JS] Download file
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