首页 > 解决方案 > Understanding which file is to big - uploading multiple files with Multer (Nodejs)

问题描述

I'v got this issue where I want to upload 3-5 images. The file size can be maximum 2.5mb in this case.

I'm using Node.js, Express and Multer and it's working great - until I need to understand which file that is above a file limit.

I get a message with "file to large" being sent as response, but I can't figure out which out of the images that was to big.

Any suggestions?

Best regards, Oscar

const maxSize = 2.5 * 1000 * 1000;
const uploader = multer({
  limits: { fileSize: maxSize },
  fileFilter: (req, file, cb) => {
    const { error } = createItemValidation(req.body);
    if (error) {
      return cb(new Error(error.details[0].message));
    } else if (
      file.mimetype == "image/png" ||
      file.mimetype == "image/jpg" ||
      file.mimetype == "image/jpeg"
    ) {
      cb(null, true);
    } else {
      cb(null, false);
      return cb(new Error("Only .png, .jpg and .jpeg format allowed!"));
    }
  },
  storage: multerS3({
    s3: s3,
    bucket: "shopitemimages",
    acl: "public-read",
    limits: { fileSize: maxSize },
    contentType: multerS3.AUTO_CONTENT_TYPE,
    metadata: function (req, file, cb) {
      cb(null, { fieldName: "lets see what we want as fieldvalue" });
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString());
    },
  }),
});

标签: node.jsexpressmultermulter-s3

解决方案


推荐阅读