question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fileFilter not working

See original GitHub issue

I wrote about this here https://github.com/expressjs/multer/issues/192

I have multer 1.0.6 , node 4.1.2, express 4.13.3

And this in file filter:

fileFilter: function (req, file, cb) {

        // The function should call `cb` with a boolean
        // to indicate if the file should be accepted

        // To reject this file pass `false`, like so:
        if (file.mimetype !== 'image/png'
            && file.mimetype !== 'image/jpg'
            && file.mimetype !== 'image/jpeg'
            && file.mimetype !== 'image/gif') {
            console.log('Got file of type', file.mimetype);
            return cb(new Error('Only image files are allowed!'));
        }

        // To accept the file pass `true`, like so:
        cb(null, true);

    }

When I send image file, everything works properly. But when I send something else like blabla.txt I expect error to return, but the request goes pending… in Chrome it says it is pending. After a couple of minutes I get response with net::ERR_EMPTY_RESPONSE.

I tried calling only

cb(new Error('Only image files are allowed!'));

without return statement, same issue.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pguerreroxcommented, Jan 18, 2017

Hello, This was happening to me, this is how I fix it,

  • From this response - Link to #186 I grabed this code, He used it to display a “FILE TO BIG” error.
app.use(function (err, req, res, next) {
  if (err.code === 'LIMIT_FILE_SIZE') {
    res.send({ result: 'fail', error: { code: 1001, message: 'File is too big' } })
    return 
  }
  // Handle any other errors
})
  • Them I added the following code:
if(err){
  console.log(err);
  res.send({ error: 'YOUR MESSAGE TO BE DISPLAY' });
}

This help me process the error created if the fileFilter is false.

hope it helps.

1reaction
benbonnetcommented, Dec 12, 2016

@srkkhan you’ve added the same wrong hint there (https://github.com/expressjs/multer/issues/114), but it is not safe to test a file by its filename extension, it has to be done by its mime type. That said, @tim-montague last answer on #114 has no real effects on my side 😕. Some has changed and things needs to be done another way ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multer: fileFilter not working - node.js - Stack Overflow
I'm using multer to upload files. everything is goung fine but fileFilter is not working. I've no idea where i'm going wrong. routes.js...
Read more >
Express multer middleware
NOTE: Multer will not process any form which is not multipart ( multipart/form-data ). ... fileFilter, Function to control which files are accepted....
Read more >
Copy files file filter not working as expected - Forum - VisualCron
In the Copy files task, on the Include file mask of the Location File filter, I have Point_UDI*.csv When I show filtered files...
Read more >
filefilter not working with runModels() (MplusAutomation)
But does not work for the inputs with a filter for the first four characters of the file (Lev1), like this: runModels("C:/ICCproj/Inputs3", recursive=TRUE ......
Read more >
Simple file filter is not working in - Bug Reports - Alfred Forum
But when I create a simple workflow with "File Filter", to this directory "~/Library/Cloudstorage/Dropbox" It does not work.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found