node.jsfilemulterfilefilter

How to catch the error when i am using file filter in multer?


I have searched but i couldn't find exact solution..When i uploading image it should allow only jpg,jpeg,gif,png..If any other file it should show message in UI. I have used the following code

var upload = multer({ storage: storage,
 fileFilter: function (req, file, cb) {
        var ext = path.extname(file.originalname);
        if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
             return cb(new Error('Wrong extension type'));
            // if(Error){
            //     console.log("error file type")
            // }

        }
        cb(null, true)
    }

});

If i try to upload pic rather than jpeg,jpg,png,git It showing error ..But how to display as message in my application page itself

Error: Wrong extension type
    at fileFilter (D:\Vishnu\octopus new\app\routes.js:912:24)
    at wrappedFileFilter (D:\Vishnu\octopus new\node_modules\multer\index.js:44:7)
    at Busboy.<anonymous> (D:\Vishnu\octopus new\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:127:13)
    at Busboy.emit (events.js:201:7)
    at Busboy.emit (D:\Vishnu\octopus new\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (D:\Vishnu\octopus new\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:96:13)
    at PartStream.emit (events.js:188:7)
    at HeaderParser.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\Dicer.js:51:16)
    at emitOne (events.js:96:13)
    at HeaderParser.emit (events.js:188:7)
    at HeaderParser._finish (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:40:12)
    at emitMany (events.js:127:13)
    at SBMH.emit (events.js:201:7)

Kindly help me in this issue.. Thanks in Advance


Solution

  • I've been struggling with this problem for a while now as well. I thought I had found one solution but it ended up not working that well for me but after enough tinkering and looking around tonight I found an answer that works for me. I hope this helps. I actually found this question while looking around for an answer.

    So what I did was created req.fileValidationError in your example as so:

    var upload = multer({ 
         fileFilter: function (req, file, cb) {
              let ext = path.extname(file.originalname);
              if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
                   req.fileValidationError = "Forbidden extension";
                   return cb(null, false, req.fileValidationError);
             }
             cb(null, true);
         }
    });
    

    Then in your route you want to check req.fileValidationError with an if statement. If it exists then you know there is a forbidden extension.

    Assuming you are using express under the app variable, and you are wanting single images to be sent, it would look something like so:

    app.post('/your-upload-route', upload.single("your-input-name-here"), function(req, res) {
         if (req.fileValidationError) {
              // return res.sendFile();
              // or return res.end();
              // or even res.render(); whatever response you want here.
         }
    });
    

    I hope this helps! If anyone else has a different way of doing this I'd be happy to update my answer as well as seeing other people's insight.