我正在设置一个服务器来从客户端上传zip文件。服务器以express和multer运行来执行此操作。上传文件时,服务器会抛出一个“MulterError:Unexpected field”错误,我无法确定是什么在扼杀它。
我尝试过png图像,它工作得很好。但是使用zip文件它只是不起作用。
const multerConfig = {
//specify diskStorage (another option is memory)
storage: multer.diskStorage({
//specify destination
destination: function(req, file, next){
next(null, './public/zip-storage');
},
//specify the filename to be unique
filename: function(req, file, next){
console.log(file);
const ext = file.mimetype.split('/')[1];
//set the file fieldname to a unique name containing the original name, current datetime and the extension.
next(null, file.fieldname + '-' + Date.now() + '.'+ext);
}
}),
// filter out and prevent non-image files.
fileFilter: function(req, file, next){
if(!file){
next();
}
// only permit zip mimetypes
const zip = file.mimetype.startsWith('application');
if(zip){
console.log('zip uploaded');
next(null, true);
}else{
console.log("file not supported")
errorReq = true;
return next();
}
}
};
/* ROUTES
**********/
app.get('/', function(req, res){
res.render('index.html');
});
var errorDetection = function(){
if(!errorReq){
errorReq = false;
return('complete.html');
} else{
errorReq = false;
return('errorupload.html');
}
}
app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
//Here is where I could add functions to then get the url of the new photo
//And relocate that to a cloud storage solution with a callback containing its new url
//then ideally loading that into your database solution. Use case - user uploading an avatar...
res.redirect('complete.html');
}
);
// RUN SERVER
app.listen(port,function(){
console.log(`Server listening on port ${port}`);
});
这是错误:
MulterError: Unexpected field
at wrappedFileFilter (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/index.js:40:19)
at Busboy.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/lib/make-middleware.js:114:7)
at emitMany (events.js:147:13)
at Busboy.emit (events.js:224:7)
at Busboy.emit (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/main.js:38:33)
at PartStream.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/types/multipart.js:213:13)
at emitOne (events.js:116:13)
at PartStream.emit (events.js:211:7)
at HeaderParser.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/dicer/lib/Dicer.js:51:16)
at emitOne (events.js:116:13)
在te代码中
app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
//Here is where I could add functions to then get the url of the new photo
//And relocate that to a cloud storage solution with a callback containing its new url
//then ideally loading that into your database solution. Use case - user uploading an avatar...
res.redirect('complete.html');
}
我把价值单('照片'),当我发送的html形式,在那里的'zip'值。更改使代码按预期工作:)