Reputation: 1048
I'm using Angular with ng2-file-upload. I'm getting an error:
{ 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: [] }
Here's my component's HTML: (The addEvent
function calls the uploadPicture
function which is further below.)
<form #f="ngForm"(ngSubmit)="addEvent(f.value)">
<input type="file" name="pic" ng2FileSelect [uploader]="uploader"/>
<button type="submit">Submit</button>
</form>
My Angular component:
uploadPicture() {
for (const item of this.uploader.queue) {
if (!item.isUploaded) {
item.upload();
}
}
}
And the uploader:
public uploader: FileUploader = new FileUploader({url: this.imgUpldURL, authToken: `Bearer ${localStorage.getItem('token')}`});
And my API end-point (I'm using a dynamic ID, if I provide the path without the dynamic ID it still doesn't work):
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');
});
});
This is all the multer configuration I have on my backend. The request is recieved successfully. Most of the issues I found online are caused by the input having a different name attribute, than the one passed in .single('name')
, however as you can see, this hasn't solved anything.
Upvotes: 0
Views: 685
Reputation: 1048
It turns out ng2-file-upload
is setting the name of all fields to file
.
Upvotes: 2