16 lines
627 B
JavaScript
16 lines
627 B
JavaScript
/*
|
|
Taken from: https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser
|
|
*/
|
|
|
|
export function downloadAsJson(exportObj, exportName) {
|
|
if (typeof exportObj !== 'string') exportObj = JSON.stringify(exportObj)
|
|
var dataStr =
|
|
'data:application/json;charset=utf-8,' + encodeURIComponent(exportObj)
|
|
var downloadAnchorNode = document.createElement('a')
|
|
downloadAnchorNode.setAttribute('href', dataStr)
|
|
downloadAnchorNode.setAttribute('download', exportName)
|
|
document.body.appendChild(downloadAnchorNode) // required for firefox
|
|
downloadAnchorNode.click()
|
|
downloadAnchorNode.remove()
|
|
}
|