Skip to content

Instantly share code, notes, and snippets.

@vishnun
Last active October 4, 2017 18:53
Show Gist options
  • Save vishnun/864dd0f50a1cce817a46f052e920489b to your computer and use it in GitHub Desktop.
Save vishnun/864dd0f50a1cce817a46f052e920489b to your computer and use it in GitHub Desktop.
Javascript save file
// ************** If you don't have server side, to download data, we can use this. **************
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