简体   繁体   English

将 data.json 文件从 postman 发送到 node.js

[英]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:我想发送一个data.json文件,存储在我的桌面上到 node.js function。我的data.json文件的结构是:

[{"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.数组中有很多 json 个对象。 Now i want to send it to my backend (node.js with express) and store it in a variable.现在我想将它发送到我的后端(带有 express 的 node.js)并将其存储在一个变量中。

I am trying like this using postman, I put the file in form data and send it to my express api.我正在尝试使用 postman,我将文件放入表单数据并将其发送到我的快递 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 {} .但它以空 object 的形式登录到控制台,就像{}一样。

I hope I explained my problem.我希望我解释了我的问题。

In HTML,在HTML,

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

In typescript,在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您通过formdata发送此表单数据

In Backend,在后端,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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