node.js - Understanding which file is to big - uploading multiple files with Multer (Nodejs)
问题描述
I'v got this issue where I want to upload 3-5 images. The file size can be maximum 2.5mb in this case.
I'm using Node.js, Express and Multer and it's working great - until I need to understand which file that is above a file limit.
I get a message with "file to large" being sent as response, but I can't figure out which out of the images that was to big.
Any suggestions?
Best regards, Oscar
const maxSize = 2.5 * 1000 * 1000;
const uploader = multer({
limits: { fileSize: maxSize },
fileFilter: (req, file, cb) => {
const { error } = createItemValidation(req.body);
if (error) {
return cb(new Error(error.details[0].message));
} else if (
file.mimetype == "image/png" ||
file.mimetype == "image/jpg" ||
file.mimetype == "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
return cb(new Error("Only .png, .jpg and .jpeg format allowed!"));
}
},
storage: multerS3({
s3: s3,
bucket: "shopitemimages",
acl: "public-read",
limits: { fileSize: maxSize },
contentType: multerS3.AUTO_CONTENT_TYPE,
metadata: function (req, file, cb) {
cb(null, { fieldName: "lets see what we want as fieldvalue" });
},
key: function (req, file, cb) {
cb(null, Date.now().toString());
},
}),
});
解决方案
推荐阅读
- php - .htaccess modrewrite 仅来自域主干
- corda - Mojave 终端没有从 runnodes 启动节点(Corda deployNodes)
- javascript - 基于光标检测句子
- sql - 使用like运算符显示数据
- node.js - 在 Angular 7 中链接多个服务调用是个好主意吗
- sql - 以字母数字顺序排列字符串 A1-1-1、A1-2-1、A1-10-1、A1-2-2、A1-2-3 等
- mysql - 使用 MySQL 对无限级分层结果集进行排序
- osgi - osgi蓝图文件的bean定义是否需要添加getter和setter
- c# - 有没有办法从一个方法多次调用不同的http请求,直到我们得到答案?
- php - Laravel Eloquent BelongTo 模型访问失败