Skip to content

Instantly share code, notes, and snippets.

@sudomaxime
Created February 26, 2018 19:53
Show Gist options
  • Save sudomaxime/76d758b22c135941e97ff60fad767bc8 to your computer and use it in GitHub Desktop.
Save sudomaxime/76d758b22c135941e97ff60fad767bc8 to your computer and use it in GitHub Desktop.
Force a file download
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE < 11
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment