Friday 21 April 2017

Upload .json file in React(client side) and send it to Node(server side)

I want for a user to be able to browse through their local files choose a json file and upload it. I dont want to import it directly. After that I would send it to some server api, and save the data from json file to database. The problem is how to upload a json file and how to send data from it to server side api?

JSON file would look like this {"people": [{"name": "Jon", age:23}, {"name": "Jane", age:25} ]}

I have a react component like this

   ...
   handleSubmit (e) {
     e.preventDefault()
     const fileUpload = this.refs.file.value;
   }

   render (): React.Element<any> {
      return (
        ...
        <form onSubmit={this.handleSubmit} encType="multipart/form-data">
          <input type="file" ref="file" accept=".json" id="fileInput" aria-describedby="fileHelp" />

        <button type="submit">Submit</button>
        </form>
    )



via Igor-Vuk

No comments:

Post a Comment