简体   繁体   中英

send a data.json file to node.js from postman

I want to send a data.json file, stored on my desktop to a node.js function. The structure of my data.json file is:

[{"key":"value"}, {same key value structure for 8000 entries}]

It's a contact book which basically stores name and contact number in both in string format and also all of these store inside an array. It's lots of json objects into array. Now i want to send it to my backend (node.js with express) and store it in a variable.

I am trying like this using postman, I put the file in form data and send it to my express api.

app.post('/upload', function (req, response) {
      console.log(data);
      response.send(data);
});

But it logs to the console as an empty object like so {} .

I hope I explained my problem.

In HTML,

<input name="file" type="file" (change)=filechanged($event)/>

In typescript,

filechanged(e) {
  this.file = e.target.files[0];
  let fileReader = new FileReader();
  fileReader.onload = (e) => {
    this.Name = fileReader.result;
    this.data = this.Name;
  };
  fileReader.readAsText(this.file);
}

submit() {
  var formdata = new FormData();
  formdata.append("file", JSON.stringify(this.data));
}

You send this formdata through API

In Backend,

app.post("/upload", function (req, response) {
  var data = JSON.parse(req.body.file);
  response.send(data);
});

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