2

我正在尝试将一组图像发送到我的后端,然后使用 multer-s3 上传它们。我通过对节点快递后端的反应发送它们。当我从前端提交时,它是一个提交图像数组,如下所示:

图像:[文件(3195869){名称:“IMG - example.JPG”,lastModified:1514505624000,lastModifiedDate:2017年12月28日星期四17:00:24 GMT-0700(MST),webkitRelativePath:“”,大小:3195869,... }, ... ]

但是当我在后端记录 req.body 它返回 [ {}, {}, {}, ... ] 并且 req.files 未定义

为什么文件不能通过发布请求?

4

2 回答 2

0

它在用法部分进行了解释^^

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files 
  // req.body will contain the text fields, if there were any 
})

就像快递 js 说的:

“在 Express 4 中,req.files 默认在 req 对象上不再可用。要访问 req.files 对象上上传的文件,请使用多部分处理中间件,例如 busboy、multer、formidable、multiparty、connect-multiparty 或 pez 。”

一个很好的教程,你想要上传多个文件的部分:https ://scotch.io/tutorials/express-file-uploads-with-multer#toc-upload-multiple-files

于 2018-01-03T21:59:44.563 回答
0

我使用了 npm 的 express-form-post 并发现它非常有用。您可以在某些 for 循环或 sumn 中使用 Object.keys(req.files[index]) 访问该文件数组,然后对它们执行任何操作.....它与 s3 非常兼容......来自表单字段是键,其他元数据(文件大小、原始名称等)是与该键一起使用的数据

于 2018-01-08T20:28:46.260 回答