Uploading a JSON file and using it

后端 未结 5 2058
深忆病人
深忆病人 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:39

    Try this, works perfect

    handleUploadFile = async(doc) => {
      let file = doc.target.files[0]
      let reader = new FileReader(file)
    
      // await reader.readAsDataURL(file)
    
      reader.readAsText(file)
    
      reader.onload = async(e) => {
    
        let aaa = e.target.result
    
        let content = await JSON.parse(aaa)
        console.log(content)
    
      }
    }
    

提交回复
热议问题