使用 Multer 快速上传多文件但未出现错误 mimetype

时间:2021-06-06 16:08:18

标签: express multer

我正在尝试从 multer 获取错误,但它给出了 no err,尽管我尝试上传未被接受的文件。

这是代码

    const storage = multer.diskStorage({
      destination: (req, file, callback) => {
        callback(null, '/public/uploads');
      },
      filename: (req, file, callback) => {
        callback(null, file.fieldname + '-' + Date.now());
      }
    });


    
    const upload = multer({
        storage,
  limits: {
    fileSize: 1024 * 1024
  },
      fileSize: 15000000, // 150 KB for a 1080x1080 JPG 90
      fileFilter: (req, file, cb) => {
        if (
          file.mimetype == 'image/png' ||
          file.mimetype == 'image/jpg' ||
          file.mimetype == 'image/jpeg' ||
          file.mimetype == 'image/gif'
        ) {
          console.log(file.mimetype);
          cb(null, true);
        } else {
          cb(null, false);
          return cb(new Error('not accepted'));
        }
      }
    }).array('photos', 2);




app.post('/matches', async (req, res) => {
  upload(req, res, function (err) {
    if (err instanceof multer.MulterError) {
      console.log(err);
      // A Multer error occurred when uploading.
    } else if (err) {
      console.log(err);
      // An unknown error occurred when uploading.
    }
    console.log('no er');
    // Everything went fine.
  });
}):

未触发 mimetype 错误.. 我不知道为什么?

0 个答案:

没有答案