Multer on Node.js throws { Error: Unexpected field... }
Multer on Node.js throws { Error: Unexpected field... }
我正在使用 Angular 和 ng2-file-upload。
我收到一个错误:
{ Error: Unexpected field
at makeError (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-error.js:12:13)
at wrappedFileFilter (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:159:13)
at Busboy.emit (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:159:13)
at HeaderParser.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:159:13)
at HeaderParser._finish (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\HeaderParser.js:68:8)
code: 'LIMIT_UNEXPECTED_FILE',
field: 'file',
storageErrors: [] }
这是我的组件的 HTML:(addEvent
函数调用下面的 uploadPicture
函数。)
<form #f="ngForm"(ngSubmit)="addEvent(f.value)">
<input type="file" name="pic" ng2FileSelect [uploader]="uploader"/>
<button type="submit">Submit</button>
</form>
我的Angular组件:
uploadPicture() {
for (const item of this.uploader.queue) {
if (!item.isUploaded) {
item.upload();
}
}
}
上传者:
public uploader: FileUploader = new FileUploader({url: this.imgUpldURL, authToken: `Bearer ${localStorage.getItem('token')}`});
还有我的 API 端点(我使用的是动态 ID,如果我提供没有动态 ID 的路径它仍然不起作用):
const multer = require('multer');
router.post('/uploadEventImage', authCheck, (req, res) => {
const id = req.body.id;
const upload = multer({dest:
`../src/assets/images/news/${id}/`}).single('pic');
console.log('called')
upload(req, res, function (err) {
if (err) {
console.log(err);
return res.end(err.toString());
}
console.log('done');
res.end('File is uploaded');
});
});
这是我后端的所有 multer 配置。成功接收请求。我在网上发现的大多数问题都是由于输入的名称属性与传入的名称属性不同 .single('name')
引起的,但是如您所见,这并没有解决任何问题。
原来 ng2-file-upload
正在将所有字段的名称设置为 file
。
我正在使用 Angular 和 ng2-file-upload。 我收到一个错误:
{ Error: Unexpected field
at makeError (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-error.js:12:13)
at wrappedFileFilter (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:159:13)
at Busboy.emit (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:159:13)
at HeaderParser.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:159:13)
at HeaderParser._finish (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\HeaderParser.js:68:8)
code: 'LIMIT_UNEXPECTED_FILE',
field: 'file',
storageErrors: [] }
这是我的组件的 HTML:(addEvent
函数调用下面的 uploadPicture
函数。)
<form #f="ngForm"(ngSubmit)="addEvent(f.value)">
<input type="file" name="pic" ng2FileSelect [uploader]="uploader"/>
<button type="submit">Submit</button>
</form>
我的Angular组件:
uploadPicture() {
for (const item of this.uploader.queue) {
if (!item.isUploaded) {
item.upload();
}
}
}
上传者:
public uploader: FileUploader = new FileUploader({url: this.imgUpldURL, authToken: `Bearer ${localStorage.getItem('token')}`});
还有我的 API 端点(我使用的是动态 ID,如果我提供没有动态 ID 的路径它仍然不起作用):
const multer = require('multer');
router.post('/uploadEventImage', authCheck, (req, res) => {
const id = req.body.id;
const upload = multer({dest:
`../src/assets/images/news/${id}/`}).single('pic');
console.log('called')
upload(req, res, function (err) {
if (err) {
console.log(err);
return res.end(err.toString());
}
console.log('done');
res.end('File is uploaded');
});
});
这是我后端的所有 multer 配置。成功接收请求。我在网上发现的大多数问题都是由于输入的名称属性与传入的名称属性不同 .single('name')
引起的,但是如您所见,这并没有解决任何问题。
原来 ng2-file-upload
正在将所有字段的名称设置为 file
。