我正在尝试使用multer将图像上传到mysql数据库,我收到错误说明
TypeError:无法读取属性'路径'未定义的
我的App.js
var multer = require('multer');
app.use(express.static("public"));
app.post("/updateImage/add",multer({ dest: './public/uploads/'}).single('img') ,contentUpdate.addImage);
我的contentUpdate.js
exports.addImage = function(req, res) {
var path = (req.file.path).replace("public/", '');
console.log(path);
var data =
{
image: path
};
var URLs = data.PageURL;
connection.query('INSERT INTO `updatedimages` SET ?', [data], function(err, rows)
{
if (err)
{
console.log(err);
} else
{
req.flash('success','Entry Successful');
return res.redirect(URLs);
}
});
};
我的updateImage.handlebars
<form id="myForm" action='/updateImage/add' method='POST' >
<div class="col-md-12" >
<input name='img' type="file" class="form-control" required/>
</div>
<div>
<button type="submit" class="glyphicon glyphicon-submit btn btn-primary ">
</div>
答案 0 :(得分:0)
更改以下内容:
<input name='img' type="file" class="form-control" required/>
到
<input name='file' type="file" class="form-control" required/>
然后尝试:
app.post("/updateImage/add",multer({ dest: './public/uploads/'}).single('file') ,contentUpdate.addImage);
答案 1 :(得分:0)
您需要在HTML中设置正确的编码类型:
<form id="myForm" action='/updateImage/add' method='POST' enctype='multipart/form-data'>
即便如此,还验证输入总是好的:
if (! req.file || ! req.file.path) {
return res.sendStatus(400);
}