最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

当我在multer中使用文件过滤器时如何捕获错误?

IT培训 admin 5浏览 0评论

当我在multer中使用文件过滤器时如何捕获错误?

我已经搜索但我找不到确切的解决方案..当我上传图像时,它应该只允许jpg,jpeg,gif,png ..如果任何其他文件它应该在UI中显示消息。我使用了以下代码

var upload = multer({ storage: storage,
 fileFilter: function (req, file, cb) {
        var ext = path.extname(file.originalname);
        if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
             return cb(new Error('Wrong extension type'));
            // if(Error){
            //     console.log("error file type")
            // }

        }
        cb(null, true)
    }

});

如果我尝试上传pic而不是jpeg,jpg,png,git它显示错误..但是如何在我的应用程序页面本身显示为消息

Error: Wrong extension type
    at fileFilter (D:\Vishnu\octopus new\app\routes.js:912:24)
    at wrappedFileFilter (D:\Vishnu\octopus new\node_modules\multer\index.js:44:7)
    at Busboy.<anonymous> (D:\Vishnu\octopus new\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:127:13)
    at Busboy.emit (events.js:201:7)
    at Busboy.emit (D:\Vishnu\octopus new\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (D:\Vishnu\octopus new\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:96:13)
    at PartStream.emit (events.js:188:7)
    at HeaderParser.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\Dicer.js:51:16)
    at emitOne (events.js:96:13)
    at HeaderParser.emit (events.js:188:7)
    at HeaderParser._finish (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:40:12)
    at emitMany (events.js:127:13)
    at SBMH.emit (events.js:201:7)

请在这个问题上帮助我..在此先感谢

回答如下:

我现在也一直在努力解决这个问题。我以为我找到了一个解决方案,但结果却不适合我,但经过足够的修修补补,今晚我找到了一个对我有用的答案。我希望这有帮助。我实际上在寻找答案时发现了这个问题。

所以我所做的是在你的例子中创建了req.fileValidationError,如下所示:

var upload = multer({ 
     fileFilter: function (req, file, cb) {
          let ext = path.extname(file.originalname);
          if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
               req.fileValidationError = "Forbidden extension";
               return cb(null, false, req.fileValidationError);
         }
         cb(null, true);
     }
});

然后在您的路由中,您要使用if语句检查req.fileValidationError。如果它存在,那么你知道有一个禁止的扩展名。

假设你在app变量下使用express,并且你想要发送单个图像,它看起来像这样:

app.post('/your-upload-route', upload.single("your-input-name-here"), function(req, res) {
     if (req.fileValidationError) {
          // return res.sendFile();
          // or return res.end();
          // or even res.render(); whatever response you want here.
     }
});

我希望这有帮助!如果其他人有不同的方式这样做,我很乐意更新我的答案以及看到其他人的见解。

当我在multer中使用文件过滤器时如何捕获错误?

我已经搜索但我找不到确切的解决方案..当我上传图像时,它应该只允许jpg,jpeg,gif,png ..如果任何其他文件它应该在UI中显示消息。我使用了以下代码

var upload = multer({ storage: storage,
 fileFilter: function (req, file, cb) {
        var ext = path.extname(file.originalname);
        if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
             return cb(new Error('Wrong extension type'));
            // if(Error){
            //     console.log("error file type")
            // }

        }
        cb(null, true)
    }

});

如果我尝试上传pic而不是jpeg,jpg,png,git它显示错误..但是如何在我的应用程序页面本身显示为消息

Error: Wrong extension type
    at fileFilter (D:\Vishnu\octopus new\app\routes.js:912:24)
    at wrappedFileFilter (D:\Vishnu\octopus new\node_modules\multer\index.js:44:7)
    at Busboy.<anonymous> (D:\Vishnu\octopus new\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:127:13)
    at Busboy.emit (events.js:201:7)
    at Busboy.emit (D:\Vishnu\octopus new\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (D:\Vishnu\octopus new\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:96:13)
    at PartStream.emit (events.js:188:7)
    at HeaderParser.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\Dicer.js:51:16)
    at emitOne (events.js:96:13)
    at HeaderParser.emit (events.js:188:7)
    at HeaderParser._finish (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (D:\Vishnu\octopus new\node_modules\dicer\lib\HeaderParser.js:40:12)
    at emitMany (events.js:127:13)
    at SBMH.emit (events.js:201:7)

请在这个问题上帮助我..在此先感谢

回答如下:

我现在也一直在努力解决这个问题。我以为我找到了一个解决方案,但结果却不适合我,但经过足够的修修补补,今晚我找到了一个对我有用的答案。我希望这有帮助。我实际上在寻找答案时发现了这个问题。

所以我所做的是在你的例子中创建了req.fileValidationError,如下所示:

var upload = multer({ 
     fileFilter: function (req, file, cb) {
          let ext = path.extname(file.originalname);
          if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
               req.fileValidationError = "Forbidden extension";
               return cb(null, false, req.fileValidationError);
         }
         cb(null, true);
     }
});

然后在您的路由中,您要使用if语句检查req.fileValidationError。如果它存在,那么你知道有一个禁止的扩展名。

假设你在app变量下使用express,并且你想要发送单个图像,它看起来像这样:

app.post('/your-upload-route', upload.single("your-input-name-here"), function(req, res) {
     if (req.fileValidationError) {
          // return res.sendFile();
          // or return res.end();
          // or even res.render(); whatever response you want here.
     }
});

我希望这有帮助!如果其他人有不同的方式这样做,我很乐意更新我的答案以及看到其他人的见解。

发布评论

评论列表 (0)

  1. 暂无评论