【问题标题】:Multer file upload error, request hangingMulter文件上传错误,请求挂起
【发布时间】:2022-08-03 02:18:36
【问题描述】:

我以前从未写过问题,因为在提问之前我总是在这里找到问题的答案。但是,我在文件上传 POST 路由上的文件过滤器功能上苦苦挣扎。我在路由中使用了 multer 上传功能,也作为中间件使用了错误处理中间件,但无法让它按我的预期运行。

我预计 \'LIMIT_UNEXPECTED_FILE\' 错误会触发闪存消息,然后重定向到选择文件上传的同一页面。这种情况有时会发生,但并不一致。如果在我的测试期间多次上传尝试连续失败,第三次或第四次请求将挂起。

我知道 fileFilter 函数正在运行,并输出正确的错误,因为我在 post 路由内的上传函数中有多个 console.log,它输出正确的错误。问题是 req.flash 然后 res.redirect 没有被触发。

当我在发布路由中使用上传功能作为中间件,然后在发布路由之后使用另一个中间件功能来处理错误时,我也遇到了 res.redirect 没有触发的问题。不确定我在这里缺少什么?

index.js


// set upload destination and filename for uploads
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, \'./public/uploads/\');
  },
  filename: (req, file, cb) => {
    const {
      originalname
    } = file;
    cb(null, `${uuid()}-${originalname}`);
  },
});


// filter by file types uploaded
const fileFilter = (req, file, cb) => {
  console.log(file.mimetype);
  if (file.mimetype.split(\'/\')[0] === \'image\') {
    cb(null, true);
  } else {
    cb(new multer.MulterError(\'LIMIT_UNEXPECTED_FILE\'), false);
  }
};

const upload = multer({
  storage,
  fileFilter,
  limits: {
    fileSize: 5 * 1024 * 1024
  }
}).single(\'document\');


// Upload route - logged in
router.post(\'/app/admin/upload\', function(req, res) {

  if (req.isAuthenticated()) {
    console.log(\'testing1\');
    upload(req, res, (err) => {
      if (err) {
        console.log(err.code);
        if (err.code === \'LIMIT_FILE_SIZE\') {
          req.flash(\'docError\', \'File upload failed due to file size\');
          res.redirect(\'back\');
        } else if (err.code === \'LIMIT_FILE_COUNT\') {
          req.flash(\'docError\', \'File upload failed due to file count\');
          res.redirect(\'back\');
        } else if (err.code === \'LIMIT_UNEXPECTED_FILE\') {
          console.log(\'testing2\');
          req.flash(\'docError\', \'File upload failed due to file type\');
          res.redirect(\'back\');
        } else {
          console.log(err.code);
          req.flash(\'docError\', \'File upload failed due to unknown error\');
          res.redirect(\'back\');
        }
      } else {
        if (!req.file) {
          req.flash(\'docError\', \'No file chosen to upload\');
          res.redirect(\'back\');
        } else {
          req.flash(\'docSuccess\', \'File uploaded successfully\');
          res.redirect(\'back\');
        }
      }
    });

  } else {
    res.redirect(\'/\');
  }
});

触发上传后路由时的服务器控制台输出

[nodemon] restarting due to changes...
[nodemon] starting `node ./bin/www`
listening on port 3000
testing12345
application/x-msdownload
LIMIT_UNEXPECTED_FILE
testing 23456

    标签: node.js express upload multer


    【解决方案1】:

    我最终需要在路由中返回上传功能。

    return upload(req, res, (err) => {

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 2015-03-12
      • 2016-03-06
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      相关资源
      最近更新 更多