unable to send multiple files using node, getting Error: unexpected field
See original GitHub issueI am using Angular ng-file-upload to upload files. When sending here is the html, but I am getting error ‘Error: Unexpected field’ and from the client getting Internal Error 500. Can you please assist with this issue?
<div class="button btn btn-danger" ngf-select ng-model="files" name="files" ngf-multiple="true">Select</div>
<button type="submit" ng-click="submit()">submit</button>
Angular as follows
if($scope.files){
console.log('upload multiple works');
console.log($scope.files);
$scope.uploadFiles($scope.files);
}
};
$scope.uploadFiles = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
Upload.upload({
url: 'api/admin/photos',
data: {file: files}
}).then(function(resp){
console.log('Successfully ' + resp.config.data.file.name);
$scope.myPic = resp.config.data.file.name;
},
function(resp){
console.log('Error status: ' + resp.status);
},
function(evt){
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
})
}
}
};
and using node
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/img')
},
filename: function (req, file, cb) {
//cb(null, file.originalname + '-' + Date.now())
cb(null, file.originalname )
}
});
var upload = multer({ storage: storage });
//upload photos
apiRoutes.post('/admin/photos',upload.array('files'),function(req,res){
console.log(req);
//save to database
console.log(req);
console.log(req.file.originalname);
console.log(req.file.originalname);
res.json(req.file[0]);
});
Issue Analytics
- State:
- Created 8 years ago
- Comments:17 (4 by maintainers)
Top Results From Across the Web
Fix "Unexpected field" Error From Multer - Maxim Orlov
When you see a MulterError: Unexpected field error, you don't suspect the reason to be the number of files exceeding the configured limit....
Read more >getting Error: Unexpected field when trying to upload multiple ...
If you upload multiple files and use upload.array('files') then your uploaded files are available in req.files , and this is an array of ......
Read more >Fix "Unexpected field" Error From Multer - YouTube
Learn how to decypher & fix this cryptic error message from multer, and finally implement working file uploads in Node.js LinksRequest ...
Read more >Node.js Express File Upload Rest API example using Multer
In this tutorial, I will show you how to upload file with Node.js Express Rest APIs to/from a static folder using Multer (with...
Read more >File upload | NestJS - A progressive Node.js framework
Warning Multer cannot process data which is not in the supported multipart format ( multipart/form-data ). Also, note that this package is not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Change your code. from :
to:
It should work.
Just use
any()
instead ofarray('file')
.When using
array('file')
, multer throws error because it expects all fields to be namedfile
, while ng-file-upload names themfile[0]
,file[1]
, etc… I wouldn’t recommend usingarrayKey: ''
because it’s basically a hack, and messes up serialization of other arrays if you are sending additional data.Another option is to use
files()
, and do something like:But then
req.files
is object with arrays of files.