Handling "File too large" properly
See original GitHub issueSo, I’m using this code:
const multer = require(`multer`);
let storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'usercontent/avatar/')
},
})
const upload = multer({
storage,
limits: {
fileSize: 2000000,
files: 1,
}
});
Now, that works. However, when a user inputs a file bigger than 2000000 bytes (2MB) the only thing that happens is an error stack in the console.
How could I display the error to the end-user by either: sending back JSON content or redirecting and silence the error in the console?
Checking if !req.file
in the POST request won’t suffice, as the field is optional and can be blank (which is expected and checked for). I tried to try/catch both the upload
variable and the post request but the error sits somewhere deeper.
If I could use a function to verify the file size before the saving occurs (just like in destination
or filename
), then this wouldn’t be a problem but is currently, as the object only takes a plain number (correct me if wrong).
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Ok, since nobody is responding and this thread is basically I’m closing it and I will attempt to fix it myself in the source code.
Hey there! I set this in my middlewares and it does work! =)