multer icon indicating copy to clipboard operation
multer copied to clipboard

limits - is there anyway to get all field limits error?

Open trungmt opened this issue 2 years ago • 1 comments

  • Let's say I have a config to limit each file's size to 5MB like this:
import multer from 'multer';
const upload = multer({
  storage: multer.memoryStorage(),
  limits: {
    fileSize: 500000, // 5MB
  }
});
  • Then I use it to handle flagIcon and flagIcon2 files
teamsRouter.post(
  '/team',
  upload.fields([
    { name: 'flagIcon', maxCount: 1 },
    { name: 'flagIcon2', maxCount: 1 },
  ]),
  adminTeamController.createTeam,
  (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ) => {
    // handle errors
    if (error instanceof MulterError) {
      return res.status(400).send(error);
    }
    res.status(500).send({ message: error.message });
  }
);
  • If I upload both of flagIcon and flagIcon2 files with larger than 5MB images. I only get one error of flagIcon thrown:
{
    "name": "MulterError",
    "message": "File too large",
    "code": "LIMIT_FILE_SIZE",
    "field": "flagIcon",
    "storageErrors": []
}
  • Question: How can I get all errors thrown? In other words, how to keep upload process (of other fields) continue in case there is error from a field?

trungmt avatar Sep 03 '21 20:09 trungmt

Sorry for the late reply. This is not currently supported since the we stop reading from the stream when we encounter the limit.

We could potentially implement this by keeping the stream open and scan it without saving the data 🤔

LinusU avatar Dec 07 '21 11:12 LinusU