Skip to content

Instantly share code, notes, and snippets.

@jbroadice
Last active September 29, 2021 15:38
Show Gist options
  • Save jbroadice/0a2f6c720cd367040a83fec7da2e91bb to your computer and use it in GitHub Desktop.
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.
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