简体   繁体   中英

Javascript - upload json and parse it

I'm building my web application using Javascript and Php, and currently want to implement such a feature, that allows user to upload (using

<input type="file" id="myFile" name="filename">

element) json file and then, by pressing some button next to it, to parse the data from this json into another json that will be saved on the server.

Which javascript features are recommended for doing this? Is it ok to go with ajax requests? Note that I don't want to upload that user's json anywhere on my server, I only want to use it as a user's input, which parsing result gets uploaded on the server.

Sure, you can read the file's content on browser side.

  let file = document.getElementById('myFile').files[0];

  let reader = new FileReader();

  reader.readAsText(file);

  reader.onload = function() {
    let data = JSON.parse(reader.result);
    ...
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM