mt-polygon-simplification/lib/json-download/index.js

16 lines
627 B
JavaScript
Raw Normal View History

2019-08-06 14:03:05 +02:00
/*
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()
}