在Node.js中使用multer的upload.fields()或upload.arrays()方法上传文件时,可能会遇到以下问题:
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.fields([{ name: 'file1', maxCount: 1 }, { name: 'file2', maxCount: 1 }]), (req, res) => {
// 处理上传的文件
});
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
app.post('/upload', upload.fields([{ name: 'file1', maxCount: 1 }, { name: 'file2', maxCount: 1 }]), (req, res) => {
// 处理上传的文件
});
const multer = require('multer');
const upload = multer({
dest: 'uploads/',
limits: { fileSize: 2 * 1024 * 1024 } // 2MB
});
app.post('/upload', upload.fields([{ name: 'file1', maxCount: 1 }, { name: 'file2', maxCount: 1 }]), (req, res) => {
// 处理上传的文件
});
总结:在Node.js中使用multer的upload.fields()或upload.arrays()方法上传文件时,需要正确配置multer中间件、存储引擎和文件大小限制,以确保文件能够成功上传并保存到指定的目录中。
腾讯云相关产品推荐:
领取专属 10元无门槛券
手把手带您无忧上云