Skip to content

Instantly share code, notes, and snippets.

@theredhead
Created August 21, 2019 10:44
Show Gist options
  • Save theredhead/94268ac347fa9573e14ed7c247b4deaa to your computer and use it in GitHub Desktop.
Save theredhead/94268ac347fa9573e14ed7c247b4deaa to your computer and use it in GitHub Desktop.
export function download(fileName: string, data: any) {
const json = JSON.stringify(data, null, 2);
const anchor = document.createElement('a');
anchor.setAttribute(
'href',
'data:text/plain;charset=utf-8,' + encodeURIComponent(json)
);
anchor.setAttribute('download', fileName);
if (document.createEvent) {
const event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
anchor.dispatchEvent(event);
} else {
anchor.click();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment