如何将文件名从 multer 传递到另一个中间件?
how to pass file names from multer to another middleware?
Multer 获取我上传的文件并将其放入文件对象中,以便我们可以通过文件访问它,但是 req.body.image
是空的,我希望能够通过 file.originalname
以 req.body.image
作为存储磁盘位置的路径。现在,我在这里处理多个文件,所以我想将它们的路径存储在 req 对象中,并在另一个中间件中访问它。
我试过这样的东西
req.body.image.push(`path/${file.originalname}`)
其中 returns 一个错误
TypeError: Cannot read property 'push' of undefined
TypeError: Cannot read property 'push' of undefined
当您使用 push()
.
时,req.body.image
似乎不是数组,而是 undefined
当 multer adds a single file to the request object, it adds them to the req.file
property. You can then access req.file
in subsequent middleware like in the below example. If you have multiple uploaded files 时,您将访问文件数组 req.files
并遍历 collection 以访问每个文件 object.
在您上面的代码中,您正在修改 req.body
object 并添加 属性 image
,我不建议更改 req.body
object。最好不要改变请求 headers 或 body 之类的东西。相反,您可以向请求 object.
添加新的 属性 req.image
下面的例子使用 Router Middleware 来封装上传逻辑,(通常应该保持在单个路由中)然后将该中间件添加到 Express Server。
imageUploadRouter.js
// imageUploadRouter.js
const router = require('express').Router()
const multer = require('multer')
const upload = multer({dest: 'uploads/'})
router.use(upload.single('image'))
router.use((req, res, next) => {
if (!Array.isArray(req.image)) {
req.image = []
}
if (req.file) {
req.image.push(`path/${req.file.originalName}`)
}
return next()
})
// add some additional routes for logic
router.post('/', (req, res) => {
// do something and respond
// you can access req.image here too
return res.sendStatus(201)
})
module.exports = router
server.js
// server.js
const express = require('express')
const ImageUploadRouter = require('./imageUploadRouter')
const server = new Express()
const port = process.env.PORT || 1337
server.use('/images', ImageUploadRouter)
server.listen(port, () => console.log(`Lisening on ${port}`))
Multer 获取我上传的文件并将其放入文件对象中,以便我们可以通过文件访问它,但是 req.body.image
是空的,我希望能够通过 file.originalname
以 req.body.image
作为存储磁盘位置的路径。现在,我在这里处理多个文件,所以我想将它们的路径存储在 req 对象中,并在另一个中间件中访问它。
我试过这样的东西
req.body.image.push(`path/${file.originalname}`)
其中 returns 一个错误
TypeError: Cannot read property 'push' of undefined
TypeError: Cannot read property 'push' of undefined
当您使用 push()
.
req.body.image
似乎不是数组,而是 undefined
当 multer adds a single file to the request object, it adds them to the req.file
property. You can then access req.file
in subsequent middleware like in the below example. If you have multiple uploaded files 时,您将访问文件数组 req.files
并遍历 collection 以访问每个文件 object.
在您上面的代码中,您正在修改 req.body
object 并添加 属性 image
,我不建议更改 req.body
object。最好不要改变请求 headers 或 body 之类的东西。相反,您可以向请求 object.
req.image
下面的例子使用 Router Middleware 来封装上传逻辑,(通常应该保持在单个路由中)然后将该中间件添加到 Express Server。
imageUploadRouter.js
// imageUploadRouter.js
const router = require('express').Router()
const multer = require('multer')
const upload = multer({dest: 'uploads/'})
router.use(upload.single('image'))
router.use((req, res, next) => {
if (!Array.isArray(req.image)) {
req.image = []
}
if (req.file) {
req.image.push(`path/${req.file.originalName}`)
}
return next()
})
// add some additional routes for logic
router.post('/', (req, res) => {
// do something and respond
// you can access req.image here too
return res.sendStatus(201)
})
module.exports = router
server.js
// server.js
const express = require('express')
const ImageUploadRouter = require('./imageUploadRouter')
const server = new Express()
const port = process.env.PORT || 1337
server.use('/images', ImageUploadRouter)
server.listen(port, () => console.log(`Lisening on ${port}`))