node.js - 如何将文件数组发送到express后端

标签 node.js reactjs express file-upload multer-s3

我正在尝试将一组图像发送到我的后端,然后使用 multer-s3 上传它们。我通过 react 将它们发送到 Node express 后端。当我从前端提交时,它是提交一组图像,如下所示:

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

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

为什么文件无法通过发布请求?

最佳答案

Usage中有解释部分^^

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 
})

就像express js所说:

"In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez."

对您来说这是一个很好的教程,其中包含您要上传多个文件的部分:https://scotch.io/tutorials/express-file-uploads-with-multer#toc-upload-multiple-files

关于node.js - 如何将文件数组发送到express后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48085568/

相关文章:

mysql - 如何在Sequelize中配置一对多关系?

javascript - 使用nodejs运行两个任务异步?

CSS 加载器 : Unexpected token of css coming from node module

node.js - Fluentd 到 elasticsearch-将数据从 fluentd 刷新到 elasticsearch 时出现错误 406(不支持 Content-Type header [])

javascript - 功能在需要时触发。我应该如何解决这个问题?

javascript - 使用React Typescript CRA设置 Electron

node.js - 如何知道文件下载何时完成?

node.js - Ubuntu 9.04 上的 npm 安装问题

node.js - ReactJS:如何在本地服务器上部署

node.js - 为什么 Express 不捆绑主体解析器?