app.post('/upload',function(req,res){
var form = new formidable.IncomingForm();
form.encoding = 'utf-8';
form.uploadDir = __dirname + '/public/uploads/';
form.keepExtensions = true;
form.maxFieldsSize = 2*1024*1024;
form.parse( req, function( err , fields , files ){
if(err){ //console.log(err);
res.redirect('admin/stageHome');
}
var filesName = req.files.fulAvatar.name, //
filesType = req.files.fulAvatar.type; if(filesName == "" ){ //
console.log('There is no file !');
return;
}
if( !(!/\.(gif|jpg|jpeg|bmp|png)$/.exec(filesName)) ){ // 检测文件格式是否为gif,jpg,jpeg,bmp,png
var suffixName = /\w{1,4}$/.exec(filesType)[0]; //获取后缀名
var avatarName = code.generateRandomCode(12) + code.gotTime() + '.' + suffixName;
var newPath = form.uploadDir + avatarName;
var resPath = req.files.fulAvatar.path;
fs.renameSync(resPath, newPath); //重命名
}
else{
console.log('不支持的文件格式 !'); r
eturn;
}
}
console.log('success!');
});
res.render('stageHome',{title:''}); ///这一句
});
加粗这一句加上去之后,图片就无法上传成功,
不加这一句图片可以上传成功,但网页一直处于响应状态或网页报错,
请高人指点如何解决!
谢过了!!!
TYGK
相关分类