fileFilter refused file still uploaded in the diskStorage
See original GitHub issuegiven a code similar to this:
const multerDiskStorage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "tmp/uploads/");
},
filename: function(req, file, cb) {
cb(null, hashFile(file));
}
});
const multerInstance = multer({
storage: multerDiskStorage,
fileFilter: (req, file, cb) => {
let error;
if (!UploadImagesController.didReceiveFile(file))
error = new Error("no file");
if (!UploadImagesController.validImageByteSize(req))
error = new Error("file bigger then permitted");
if (!UploadImagesController.validImageType(file))
error = new Error("file type not permitted");
if (error) {
cb(error, false);
}
cb(null, true);
},
limits: {
fieldSize: 20000000,
fileSize: 20000000
}
});
The fileFilter works as expected except for the fact that when a fileFilter cb with error
and false
is invoked the file still gets uploaded in the tmp/uploads
folder (with a name that is not the result of invoking the filename
callback propriety defined in the storage)
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
fileFilter on multer still allowing all file types - Stack Overflow
Added a fileFilter to my storage function allowing only image file types to be uploaded, but all file types are still being uploaded....
Read more >Express multer middleware
Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files. It is written on top of busboy...
Read more >Uploading Files Using Multer in a Node.js Application
In this article, we will see how to use Multer to handle multipart/form-data using Node.js, Express and MongoDB.
Read more >"ENOENT: no such file or directory, open 'E:\\astrology\\utils ...
diskStorage ({ destination: function (req, file, cb) { cb(null, ... const fileFilter = (req, file, cb) => { // reject a file if ......
Read more >Help with Multer package and image path : r/node - Reddit
image uploading const multer = require('multer') const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, '.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Based on @HarshithaKP insight
I solved the problem the following way:
cb
with only one parameterError
instance to cb, actually passed a string (passing the Error instance resulted in losing the message!!!)this seems to have solved the problem.
Based on this I think that the readme needs to be changed to correctly state the required syntax and gotchas to make this work!
https://github.com/expressjs/multer#filefilter
@HarshithaKP Ty a lot for it, I will try it our ASAP,
From what i was able to infer the problem is invoking the
cb
with two arguments instead of invoking it only with the first argument?