gpt4 book ai didi

node.js - Multer:文件过滤器不工作

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:50 24 4
gpt4 key购买 nike

我正在使用 multer 上传文件。一切正常,但 fileFilter 不工作。我不知道哪里出错了。

routes.js

var multer = require('multer');
// Multer diskStorage setting
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/');
},
filFilter: function (req, file, cb) {
var typeArray = file.mimetype.split('/');
var fileType = typeArray[1];
if (fileType == 'jpg' || fileType == 'png') {
cb(null, true);
} else {
cb(null, false)
}
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '_' + Date.now() + '.jpg');
}
});

var upload = multer({ storage: storage });
router.post('/add', upload.single('photo'), function(req, res){

console.log('uploaded');
}

Multer 还会上传不允许的文件。有什么想法吗?

最佳答案

const storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/');
},
fileFilter: function (req, file, cb) {
const extension = path.extname(file.originalname).toLowerCase();
const mimetyp = file.mimetype;
if (
extension !== '.jpg' ||
extension !== '.jpeg' ||
extension !== '.png' ||
mimetyp !== 'image/png' ||
mimetyp !== 'image/jpg' ||
mimetyp !== 'image/jpeg'
) {
cb('error message', true);
}
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '_' + Date.now() + '.jpg');
},
});

const upload = multer({ storage: storage });
router.post('/add', upload.single('photo'), function (req, res) {
console.log('uploaded');
});

我认为这会有所帮助

关于node.js - Multer:文件过滤器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48744421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com