multer: how to pass variables to next handler
我正在使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import multer from"multer"; import * as mime from"mime-types"; import path from"path"; export const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null,"uploads/"); }, filename: function (req: any, file, cb) { const name = path.parse(file.originalname).name +"_" + Date.now(); const extension = mime.extension(file.mimetype); const filename = `${name}.${extension}`; /** req.fileInfo = { filename, name, extension } */ cb(null, filename); }, }); export const upload = multer({ storage: storage }); |
我想在下一个处理程序中获取
1 2 3 4 5 6 7 8 | import { upload } from"src/middlewares/imageUpload"; router.post('/imageUpload', upload.single("upload"), async (req, res) => { // const filename = req.fileInfo.filename; }); |
根据这个答案,
但 multer diskStorage 配置不接受
如何将
如果您仍然想这样做,您可以将
您可以使每个请求都通过