Uploading a JSON file and using it

后端 未结 5 2051
深忆病人
深忆病人 2020-12-13 16:00

How can I upload a JSON file on some click on a button on my web page say \"import\", and use it to store in a variable to use and update it using JavaScript.

I have

5条回答
  •  Happy的楠姐
    2020-12-13 16:49

    Basic upload File:

        
    
      document.getElementById('contentFile').onchange = function(evt) {
            try {
                let files = evt.target.files;
                if (!files.length) {
                    alert('No file selected!');
                    return;
                }
                let file = files[0];
                let reader = new FileReader();
                const self = this;
                reader.onload = (event) => {
                    console.log('FILE CONTENT', event.target.result);
                };
                reader.readAsText(file);
            } catch (err) {
                console.error(err);
            }
        }
    
                         ` 
    

提交回复
热议问题