Created
February 26, 2018 19:53
-
-
Save sudomaxime/76d758b22c135941e97ff60fad767bc8 to your computer and use it in GitHub Desktop.
Force a file download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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