【问题标题】:how to read posted data on node js api如何读取节点 js api 上的发布数据
【发布时间】:2018-11-16 12:30:39
【问题描述】:

我想读取上传到 backened 的 csv 数据。

为此,我从前端通过帖子发送数据..

前端代码:

fileEvent(e) {
    this.filedata = e.target.files;
    if (this.filedata.length > 0) {
      const file: File = this.filedata[0];
      console.log(file);
      const formData: FormData = new FormData();
      formData.append('files', file, file.name);
      this.http.post('myUrl',  {file: formData}, this.options)
                     .subscribe((res) => {
                     });
    }
  } 

我的文件截图:

现在我已经在 api.js 上写了路线来指导我 到我创建的控制器。

我的 api.js 代码:

router.post('/product/csvdata', function (req, res) {
    productimport.importcsvProduct(req, res);
});

最后在我的控制器上,我正在安慰我的数据:

    var product = {
            importcsvProduct: function (req,res) {
                console.log(req.body.file);
             }
    };

module.exports = product;

但我在控制台中得到了空的 {}..??

任何人都可以检查这是什么问题..??

【问题讨论】:

  • 你可能需要body-parser作为中间件并配置它来接受表单数据
  • this 能帮你解决问题吗?

标签: node.js angular rest api form-data


【解决方案1】:

在这种情况下,您需要使用文件处理中间件,例如multer

const express = require('express')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('csvdata'), function (req, res, next) {
  // req.file is the `csvdata` file
  // req.body will hold the text fields, if there were any
})

【讨论】:

  • 显示无法在前端发布 /api/product/csvdata
  • 你能用你正在使用的新代码更新问题吗?
猜你喜欢
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 2015-05-14
相关资源
最近更新 更多