如何从 multer.array 函数验证多个文件的 mimetype?
How to verify the mimetype of multiple files from a multer.array function?
我已经为此工作了几个小时,但仍然想不出解决方案!我的原始代码是检查使用 multer 上传的单个图像的 mimetype,它运行良好,但我无法将此代码应用于多个图像。我不确定我是否以正确的方式处理这个问题,可能需要想出一种新方法。请看下面的代码:
Multer 中间件
const multerOptions = {
storage: multer.memoryStorage(),
fileFilter(req, file, next) {
const isPhoto = file.mimetype.startsWith('image/');
if(isPhoto) {
next(null, true);
} else {
next({ message: 'That filetype isn\'t allowed!'}, false);
}
}
};
独创的单图倍增功能
exports.upload = multer(multerOptions).single('photo');
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1];
req.body.photo = `${uuid.v4()}.${extension}`;
// resize the image
const photo = await jimp.read(req.file.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
next();
};
新的多图多图功能
exports.upload = multer(multerOptions).array('photo',[5]);
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.files) {
next(); // skip to the next middleware
return;
}
imageArray = req.files;
const extensions = [];
for (var i = 0; i < imageArray.length; i++) {
imageArray[i].mimetype.split('/')[1].push(extensions);
}
// I HAVEN'T MADE IT PAST THIS LINE YET AND AM STILL STUCK ABOVE
req.body.photo = `${uuid.v4()}.${extension}`;
// resize the image
const photo = await jimp.read(req.files.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
next();
};
很难确切地知道哪里出了问题,但是通过使用 async await,您不能简单地将您的单个工作示例包装在一个 for 循环中吗?例如:
imageArray = req.files;
for (var i = 0; i < imageArray.length; i++) {
const file = imageArray[i]
const extension = file.mimetype.split('/')[1]
const name = `${uuid.v4()}.${extension}`
// resize the image
const photo = await jimp.read(file.buffer)
await photo.resize(800, jimp.AUTO)
await photo.write(`./public/uploads/${name}`)
}
next()
这实际上就是您在工作的单个示例中所拥有的,但是包含在一个循环中。
我已经为此工作了几个小时,但仍然想不出解决方案!我的原始代码是检查使用 multer 上传的单个图像的 mimetype,它运行良好,但我无法将此代码应用于多个图像。我不确定我是否以正确的方式处理这个问题,可能需要想出一种新方法。请看下面的代码:
Multer 中间件
const multerOptions = {
storage: multer.memoryStorage(),
fileFilter(req, file, next) {
const isPhoto = file.mimetype.startsWith('image/');
if(isPhoto) {
next(null, true);
} else {
next({ message: 'That filetype isn\'t allowed!'}, false);
}
}
};
独创的单图倍增功能
exports.upload = multer(multerOptions).single('photo');
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1];
req.body.photo = `${uuid.v4()}.${extension}`;
// resize the image
const photo = await jimp.read(req.file.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
next();
};
新的多图多图功能
exports.upload = multer(multerOptions).array('photo',[5]);
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.files) {
next(); // skip to the next middleware
return;
}
imageArray = req.files;
const extensions = [];
for (var i = 0; i < imageArray.length; i++) {
imageArray[i].mimetype.split('/')[1].push(extensions);
}
// I HAVEN'T MADE IT PAST THIS LINE YET AND AM STILL STUCK ABOVE
req.body.photo = `${uuid.v4()}.${extension}`;
// resize the image
const photo = await jimp.read(req.files.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
next();
};
很难确切地知道哪里出了问题,但是通过使用 async await,您不能简单地将您的单个工作示例包装在一个 for 循环中吗?例如:
imageArray = req.files;
for (var i = 0; i < imageArray.length; i++) {
const file = imageArray[i]
const extension = file.mimetype.split('/')[1]
const name = `${uuid.v4()}.${extension}`
// resize the image
const photo = await jimp.read(file.buffer)
await photo.resize(800, jimp.AUTO)
await photo.write(`./public/uploads/${name}`)
}
next()
这实际上就是您在工作的单个示例中所拥有的,但是包含在一个循环中。