Last active
January 23, 2018 14:49
-
-
Save chauek/45daed428a166e2d4db373eaa8622114 to your computer and use it in GitHub Desktop.
Triggering saveAs from browser. https://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-when-using-data-uri
This file contains 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 saveAs(uri, filename) { | |
var link = document.createElement('a'); | |
if (typeof link.download === 'string') { | |
link.href = uri; | |
link.download = filename; | |
//Firefox requires the link to be in the body | |
document.body.appendChild(link); | |
//simulate click | |
link.click(); | |
//remove the link when done | |
document.body.removeChild(link); | |
} else { | |
window.open(uri); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment