本文介绍了Multer意外字段,即使字段名称相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要从不同的字段上传多个文件.当我通过邮递员上传文件时,Multer失败,出现意外字段错误.即使我进行了检查,我的字段名称也与我在multer中使用的字段名称完全相同.
I am uploading multiple files from different fields.when i upload file via Postman, Multer fails with Unexpected field error. even though i checked, my field names are exactly same with which i am using in multer.
这是我的例外:
MulterError: Unexpected field
at wrappedFileFilter (F:\Node-Installments-Management\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (F:\Node-Installments-Management\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:182:13)
at Busboy.emit (F:\Node-Installments-Management\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (F:\Node-Installments-Management\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:182:13)
at HeaderParser.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:182:13)
at HeaderParser._finish (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:68:8)
at SBMH.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:40:12)
at SBMH.emit (events.js:182:13)
at SBMH._sbmh_feed (F:\Node-Installments-Management\node_modules\streamsearch\lib\sbmh.js:159:14)
at SBMH.push (F:\Node-Installments-Management\node_modules\streamsearch\lib\sbmh.js:56:14)
at HeaderParser.push (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:46:19)
at Dicer._oninfo (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:197:25)
at SBMH.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:127:10)
这是我的multer中间件
This is my multer middleware
const multer = require('multer');
const MIME_TYPE_MAP = {
'image/png': 'png',
'image/jpeg': 'jpg',
'image/jpg': 'jpg'
};
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const isValid = MIME_TYPE_MAP[file.mimetype];
let error = new Error('Invalid mime type');
if (isValid) {
error = null;
}
cb(error, req.uploadPath);
},
filename: (req, file, cb) => {
const name = file.originalname.toLocaleLowerCase().split(' ').join('-');
const ext = MIME_TYPE_MAP[file.mimetype];
cb(null, name + '-', + Date.now() + '.' + ext);
}
});
module.exports = multer({storage: storage}).fields([{name: 'idCardPhotoFrontPath', maxCount: 1}, {name: 'idCardPhotoBackPath`', maxCount: 1}])
这是我的邮递员
推荐答案
我猛地撞了个头,最后发现了一个错误/类型
I was banging around my head, and finally found a mistake/type
{名称:"idCardPhotoBackPath`",maxCount:1}< ===文件名带有此勾号
{name: 'idCardPhotoBackPath`', maxCount: 1}<=== file name has this back tick
这篇关于Multer意外字段,即使字段名称相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!