multer
multer copied to clipboard
fileFilter not working
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.
Hmm, are all the tests passing for you? Could you please try and clone this repo and run npm test
?
44 passing (552ms) 8 failing
1) Disk Storage should process parser/form-data POST request:
Uncaught AssertionError: 1803 == 1778
+ expected - actual
-1803
+1778
2) Disk Storage should process multiple files:
Uncaught AssertionError: 128 == 122
+ expected - actual
-128
+122
3) Functionality should upload the file to the `dest` dir:
Uncaught AssertionError: 1803 == 1778
+ expected - actual
-1803
+1778
4) Functionality should rename the destination directory to a different directory:
Uncaught AssertionError: false == true
+ expected - actual
-false
+true
5) Memory Storage should process multipart/form-data POST request:
Uncaught AssertionError: 1803 == 1778
+ expected - actual
-1803
+1778
6) Memory Storage should process multiple files:
Uncaught AssertionError: 128 == 122
+ expected - actual
-128
+122
7) Reuse Middleware should accept multiple requests:
Uncaught AssertionError: 1803 == 1778
+ expected - actual
-1803
+1778
8) Unicode should handle unicode filenames:
Uncaught AssertionError: 1803 == 1778
+ expected - actual
-1803
+1778
A strange thing happened as I changed all files in the repo to have LF line separators... at first they had CRLF as I cloned repo to my machine... anyway, now I have only 1 test failing
1) Functionality should rename the destination directory to a different directory:
Uncaught AssertionError: false == true
+ expected - actual
-false
+true
at C:\Users\Win7\Documents\Projects\multer\test\functionality.js:132:14
Any clues... so far ? :/ I am really bothered by this :(
Hi tibortru, I had the same problem. Finally my solution was to give up multer's fileFilter and do the mimetype check on the temporary file inside app.post() . Files that do not pass the check are deleted. My code looks like this:
app.post('/upload/*', upload.single('image'), function(req, res, cb) { if(path.extname(req.file.originalname).toLowerCase() !== '.jpeg') { fs.unlinkSync(req.file.path) //delete temporary file } ... }
This worked for me:
fileFilter: function (req, file, cb) {
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('Only image files are allowed!'));
}
cb(null, true);
}
@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 ?
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.
const upload = multer({
fileFilter:(req,file,cb) => {
let extname = file.originalname.toLowerCase().match(/.(jpeg|jpg|png|gif)$/);
let mimetype = file.mimetype.match(/(jpeg|jpg|png|gif)$/);
if(mimetype && extname) cb(null,true);
else cb('Error: Images Only');
},
storage:multer.diskStorage({
destination:'./public/img/',
filename:(req,file,cb) => cb(null,PKred_${Date.now()+''+(Math.floor(Math.random()*100000))}
),
limits:{ fileSize:1000000 }
})
}).single('file');
still wondering what this line does if (err.code === 'LIMIT_FILE_SIZE') {
@pguerrerox
still wondering what this line does
if (err.code === 'LIMIT_FILE_SIZE') {
@pguerrerox
if there an error, the "err" object have a "code" key. That line just check if the code throw in the error is a size related.