我尝试在上传到服务器后记录文件的路径但返回以下错误:
TypeError: Cannot read property 'path' of undefined
我设法上传文件但无法获取文件路径。
router.post('/uploadFile',function(req,res,next){
if(req.method == "POST"){
var accessToken = req.query["access_token"];
var customerID = req.decoded.userID;
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./public/images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
console.log(req.file.path);
}
});
var upload = multer({
storage: Storage
}).array("imgUploader", 3);
});
答案 0 :(得分:1)
您没有正确使用multer。
如果您正在使用数组,则会上传多个文件并使用相同的参数名称,因此应使用req.files
,而不是req.file
。结合这些,您的代码变为:
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./public/images");
},
filename: function(req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
console.log(req.files['imgUploader'][0].path); //Or use a loop for all files
}
});
var upload = multer({
storage: Storage
}).array("imgUploader", 3);
router.post('/uploadFile', upload, function(req,res,next){
if(req.method == "POST"){
[...]
}
});