Multiple files are being posted to nodejs application using Express and bodyParser.
The number of files change in every request. It is required to get number of files posted at the server.
I have tried using
req.files.length
but it gives undefined. How can I know the number of files posted in a post request? Also how to loop through each file?
Putting answer my self.
It should be iterated upon like this
for (x in files){
//code for handling each object in json.
}
You can get the length of and iterate a collection of files with the name
of the <input>
:
// example: <input type="file" name="images" multiple>
req.files.images.length;
req.files.images.forEach(function (file, i) {
// ...
});
You can find an example of this in the Connect repository: examples/upload.js