Last active
September 29, 2021 15:38
-
-
Save jbroadice/0a2f6c720cd367040a83fec7da2e91bb to your computer and use it in GitHub Desktop.
JavaScript util to saveData (download as a browser attachment), taking data and allowing output filename.
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
const saveDataAsAttachment = (() => { | |
const a = document.createElement('a'); | |
a.style = 'display: none'; | |
document.body.appendChild(a); | |
return (data, fileName) => { | |
const blob = new Blob([data], {type: 'octet/stream'}); | |
const url = window.URL.createObjectURL(blob); | |
a.href = url; | |
a.download = fileName; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment