Uploading a JSON file and using it

后端 未结 5 2046
深忆病人
深忆病人 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条回答
  •  無奈伤痛
    2020-12-13 16:40

    I have got a way to use the uploaded json file, here is the way i found.

    $("#inputFile").change(function(e) {
        onChange(e);
    });
    
    function onChange(event) {
        var reader = new FileReader();
        reader.onload = onReaderLoad;
        reader.readAsText(event.target.files[0]);
    }
    
    function onReaderLoad(event){
        //alert(event.target.result);
        var obj = JSON.parse(event.target.result);
        alert(obj);
    }
    

提交回复
热议问题