Created
August 21, 2019 10:44
-
-
Save theredhead/94268ac347fa9573e14ed7c247b4deaa to your computer and use it in GitHub Desktop.
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
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