How can I load a JSON file with some click on a button on my web page, I will say "import", and use it to store in a variable that needs to be used and updated using JavaScript.
I read other posts but could not find an answer.
I save the JSON variable using this function:
function save(filename, data){
if(!data) {
alert('error : No data')
return;
}
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
This works great, and it loads the file when another export button is clicked. How to load this file back and make a JSON data variable for this file?
source
share