Multer文件过滤器无法正常工作NodeJS

时间:2019-03-13 14:16:50

标签: node.js multer

所以我想做的就是让用户仅上传jpg jpegpng文件。但是,我现在拥有的代码仍然让我上载gif个文件。代码如下:

let storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/public/images')
    },
    filename: function (req, file, cb) {
        crypto.pseudoRandomBytes(16, function (err, raw) {
            cb(null, raw.toString('hex') + Date.now() + '.' + mime.getExtension(file.mimetype));
        });
    }
});
let upload = multer({
    limits: {
        fileSize: 1000000
    },
    fileFilter: function(req, file, cb) {
        if(!file.originalname.match(/\.(jpg|jpeg|png)$/)){
            return cb('File must be an image.');
        }
        cb(undefined, true);
    },
    storage: storage
});

路线:

app.post('/post', upload.any(), recaptcha.middleware.verify, (req, res) => {
    img: req.files[0].filename,
});

为什么验证无效?

0 个答案:

没有答案