Skip to content

Instantly share code, notes, and snippets.

@noherczeg
Created February 27, 2018 12:23
Show Gist options
  • Save noherczeg/5799d9e3e3f49f30e94b35fed90ff53a to your computer and use it in GitHub Desktop.
Save noherczeg/5799d9e3e3f49f30e94b35fed90ff53a to your computer and use it in GitHub Desktop.
var filename = "";
var type = xhr.getResponseHeader('Content-Type');
var blob = new Blob([response], {type: type});
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
setTimeout(function () {
URL.revokeObjectURL(downloadUrl);
}, 100); // cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment