【发布时间】: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