How to limit the file size when uploading with multer? How to limit the file size when uploading with multer? express express

How to limit the file size when uploading with multer?


There is no onFileUploadStart with the new multer API. If you want to limit the file size, you should instead add limits: { fileSize: maxSize } to the object passed to multer():

var upload = multer({  storage: storage,  limits: { fileSize: maxSize }}).single('bestand');


I think it is you are looking.Have a great day.

const fileFilter = (req, file, cb) => {const fileSize = parseInt(req.headers['content-length']);if((file.mimetype === 'image/png' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg' || file.mimetype === 'application/octet-stream') && (fileSize <= 1282810)) {cb(null, true);} else if(file.mimetype === 'video/mp4' && fileSize <= 22282810) {cb(null, true);}else {cb(null, false);}

};