我在 Node.js 应用程序中使用 multer 中间件验证文件大小时遇到问题。我已经实现了一个自定义中间件
setFileLimit
来根据文件类型设置文件大小限制,但执行中间件时似乎req.file
和req.files
未定义。
在将所选媒体上传到 multer 之前无法验证文件大小
前端代码:
// 1. Frontend code that sends the media
if (file) {
const formData = new FormData()
formData.append('medias', file)
console.log('send rrrr--');
console.log('type of formdata--', typeof formData);
const mediatoCloudinary = await uploadMedia(formData)
if (mediatoCloudinary?.status === 400) {
console.log('media upload error', mediatoCloudinary);
setMediaError('No media file found.')
}
}
export const uploadMedia = async(formData) => {
try {
console.log('rrr--', formData);
const result = await axiosApi.post(messagesRoute.sendMedia, formData)
console.log('media link', result);
return result.data
} catch (error) {
console.log('media error', error.message);
return error.response
}
}
//2.Backend Route:
router.post('/sendMedia', setFileLimit, multerMid.single('medias'), catchAsyncErrors((req: Req, res: Res, next: Next) => {
console.log('in send media route');
messageController.saveMediaToCloudinary(req, res, next)
}))
//3.Multer Middleware and setFileLimit function in backend:
// Custom middleware to set limits based on file type using startsWith
const setFileLimit = (req: Req, res: Res, next: Next) => {
// Access the file using the correct key ('medias')
console.log('Accessing file:', req.file, 'req.files--', req.files);
const file = req.file
if (!file) {
return res.status(400).send('No media file found.');
}
let maxSize;
if (file.mimetype.startsWith('image/')) {
maxSize = 5 * 1024 * 1024; // 5 MB for images
} else if (file.mimetype.startsWith('video/')) {
maxSize = 10 * 1024 * 1024; // 10 MB for videos
} else {
maxSize = 5 * 1024 * 1024; // Default to 5 MB for other documents
}
if (file.size > maxSize) {
return res.status(400).json({ error: 'File size exceeds the limit.' });
}
next()
}
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadDirectory = path.join(__dirname, '../public/media')
if (!fs.existsSync(uploadDirectory)) {
fs.mkdirSync(uploadDirectory, { recursive: true });
}
cb(null, uploadDirectory);
},
filename: (req, file, cb) => {
console.log('file in multer--', file);
const uniqueSuffix = uuidv4() + '-' + file.originalname
cb(null, uniqueSuffix);
},
});
const multerMid = multer({
storage: storage,
limits: {
fieldNameSize: 100,
fieldSize: 5 * 1024 * 1024
}
});
export { multerMid, setFileLimit }
尝试重新安排中间件,以便在 setFileLimit 之前执行 multerMid.single('medias')
router.post('/sendMedia', multerMid.single('medias'), setFileLimit,catchAsyncErrors((req: Req, res: Res, next: Next) => { console.log('in send media route'); messageController.saveMediaToCloudinary(req, res, next)}))