Skip to content

Instantly share code, notes, and snippets.

@priyanshujain
Last active July 28, 2018 17:17
Show Gist options
  • Save priyanshujain/85c4ea8920e952915e9d72a2ffd841f3 to your computer and use it in GitHub Desktop.
Save priyanshujain/85c4ea8920e952915e9d72a2ffd841f3 to your computer and use it in GitHub Desktop.
Download data as file in browser
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
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