Node.js 当我使用multer和html输入表单上传图像时,req.file是未定义的
我想使用nodeJS和multer上传图像,所以我做了以下工作: 以下是我的multer配置:Node.js 当我使用multer和html输入表单上传图像时,req.file是未定义的,node.js,forms,file-upload,postman,multer,Node.js,Forms,File Upload,Postman,Multer,我想使用nodeJS和multer上传图像,所以我做了以下工作: 以下是我的multer配置: var multer = require('multer'); var storage = multer.diskStorage({ //Setting up destination and filename for uploads destination: function (req, file, cb) { cb(null, 'uploads/');
var multer = require('multer');
var storage = multer.diskStorage({
//Setting up destination and filename for uploads
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname);
}
});
var upload = multer({
storage: storage,
limits:{
fieldSize: 1024*1024*6,
}
});
以下是我上传图片的路线:
router.post('/designer', upload.single('designerImage'), async (req, res) => {
console.log(req.file);
//rest of the code which is not needed for my query
})
当我使用POSTMAN使用file类型的表单数据键发布文件时,它工作得非常好。但是当我尝试使用HTML表单输入发送它时,req.file
显示为未定义
,并且没有文件上传到uploads文件夹。下面是我的HTML表单代码:
<form action="/designer" method="POST">
<input type="file" name="designerImage">
<form>
这个问题的解决办法是什么?我已花费数小时,但无法找到解决方案。multer
multipart/form data
请求,因此您需要向表单中添加enctype=“multipart/form data”
multer
multipart/form data
请求,因此您需要向表单中添加enctype=“multipart/form data”