app.post('/uploadImg’,function(req, res, next){ console.log(req.body.localUrl);//成功 console.log(req.files.imgFile.path);//成功
var form = new formidable.IncomingForm();
form.keepExtensions = true;//保留后缀 .jpg/.png
var folderName = new Date().Format('yyyy-MM-dd');
var dirPath = __dirname+"/../public/uploadimg/"+folderName+"/";
console.log(dirPath);
if(!fs.existsSync(dirPath)){//如果没有这个文件夹就创建文件夹
fs.mkdirSync(dirPath);
}
form.uploadDir = dirPath;//设置文件上传的目录
form.parse(req, function(err, fields, files){
console.log(files.imgFile);//问题出现在这里,怎么返回的是undefined?
var filePath = files.imgFile.path.replace(/\\/g, '/');
var resPath = '/uploadimg/'+folderName+'/'+filePath.substring(filePath.lastIndexOf('/')+1);
res.send({"error":0,"url":resPath});
})
})
4 回复