Search code examples
node.jsmulter

file filter function is not working in multer?


I am uploading single file using multer.fileFilter function is not working,it is not even being called by multer.Multer does not care for file extension whether they are allowed or not and upload all files. Every thing from frontend is correct like(multipart/form-data, and name of input field is image). Here is the my code

This is filefilter functon:

const filefilter = (req, file, cb) =>{
    console.log('filefilter executed');
    if (
         file.mimetype === 'image/png' || 
         file.mimetype === 'image/jpg' || 
         file.mimetype === 'image/jpeg'
    ) {
        cb(null, true);
        console.log('image type correct');
    }else {
        console.log('image type incorrect');
        cb('error Message', false);
    }
}

and here is how I used it in multer:

app.use(multer({storage : fileStorage, filefilter: filefilter }).single('image'));

Solution

  • It was Spelling mistake. In ' filefilter ' key 'f' should be capital, so instead of using:

    app.use(multer({storage : fileStorage, filefilter: filefilter }).single('image'));

    use following:

    app.use(multer({storage : fileStorage, fileFilter: filefilter }).single('image'));