I'm trying to implement multer fileFilter function and it works (it does filter files and files get uploaded) but when I set file filtering req.files becomes empty.
const extFilter = function (req, file, cb) {
if(file.originalname.match(/^.*\.(avi|mov|mp4)$/)) cb(null, true)
cb(null, false)
}
const upload = multer({ storage: cloudStorage,
fileFilter: extFilter }).array('file')
upload(req, res, function (err) {
return res.status(200).send(req.files) //returns nothing
})
Without fileFilter option set the function returns array of uploaded files.
Any ideas?
The fileFilter function was missing return before cb(null, true)
const extFilter = function (req, file, cb) {
if(file.originalname.match(/^.*\.(avi|mov|mp4)$/)) return cb(null, true)
cb(null, false)
}
Otherwise the files get written to the disk because of the first callback but then nothing is returned in req.files because of the second cb(null, false).