所以我想做的就是让用户仅上传jpg
jpeg
或png
文件。但是,我现在拥有的代码仍然让我上载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,
});
为什么验证无效?