无法使multer filefilter错误处理工作

时间:2016-01-27 23:21:39

标签: node.js express multer

我在node.js / multer

中玩文件上传

我得到了存储并限制了工作。但现在我正在使用filefilter来简单地拒绝mimetype这样的文件:

fileFilter: function (req, file, cb) {
 if (file.mimetype !== 'image/png') {
  return cb(null, false, new Error('goes wrong on the mimetype'));
 }
 cb(null, true);
}

当上传的文件不是png时,它不会接受它。 但它也不会触发if(err)

当文件很大时,它确实会产生错误。所以不知怎的,我需要在filefilter上生成一个错误,但我不确定如何猜测new Error是错误的

那么如果文件不正确,我该如何生成错误呢?我做错了什么?

完整代码:

var maxSize = 1 * 1000 * 1000;

var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, 'public/upload');
  },
  filename: function (req, file, callback) {
    callback(null, file.originalname);
  }
});


var upload = multer({
   storage : storage,
   limits: { fileSize: maxSize },
   fileFilter: function (req, file, cb) {
     if (file.mimetype !== 'image/png') {
       return cb(null, false, new Error('I don\'t have a clue!'));
     }
     cb(null, true);
   }

 }).single('bestand');


router.post('/upload',function(req,res){
    upload(req,res,function(err) {
        if(err) {
              return res.end("some error");
        }
    )}
)}

4 个答案:

答案 0 :(得分:10)

fileFilter函数可以访问请求对象(req)。您的路由器也可以使用此对象。

因此,在fileFitler中,您可以添加带有验证错误或验证错误列表的属性(您可以上传许多文件,其中一些文件可以通过)。 在路由器中,检查是否存在有错误的属性。

过滤器中的

fileFilter: function (req, file, cb) {
 if (file.mimetype !== 'image/png') {
  req.fileValidationError = 'goes wrong on the mimetype';
  return cb(null, false, new Error('goes wrong on the mimetype'));
 }
 cb(null, true);
}
路由器中的

router.post('/upload',function(req,res){
    upload(req,res,function(err) {
        if(req.fileValidationError) {
              return res.end(req.fileValidationError);
        }
    )}
)}

答案 1 :(得分:1)

更改fileFilter并将错误传递给cb函数:

function fileFilter(req, file, cb){
   const extension = file.mimetype.split('/')[0];
   if(extension !== 'image/png'){
       return cb(new Error('Something went wrong'), false);
    }
    cb(null, true);
};

答案 2 :(得分:1)

fileFilter 回调应该是:

fileFilter: async (req, file, cb) => {
    if (file.mimetype != 'image/png') {
        cb(new Error('goes wrong on the mimetype!'), false);
    }
    cb(null, true);
}

请求中的错误处理:

  • 使用 MulterError 访问 multer 错误
const multer = require("multer");

router.post('/upload', function(req, res) {
    upload(req, res, function(err) {

        // FILE SIZE ERROR
        if (err instanceof multer.MulterError) {
            return res.end("Max file size 2MB allowed!");
        }

        // INVALID FILE TYPE, message will return from fileFilter callback
        else if (err) {
            return res.end(err.message);
        }

        // FILE NOT SELECTED
        else if (!req.file) {
            return res.end("File is required!");
        }
 
        // SUCCESS
        else {
            console.log("File uploaded successfully!");
            console.log("File response", req.file);
        }

    )}
})

答案 3 :(得分:0)

SELECT distinct on (t.post_id) post_id, t.group_id, t.id, t.created_at,
       (CASE WHEN t.post_id IN (1, 2, 78) AND user_id = 100 THEN 'true' ELSE 'false' END) AS seen
FROM timelines t  join 
     post_views pv 
     USING(post_id)
WHERE t.group_id IN (1, 2, 78);