<form name='picForm' action = "javascript:;"method="post" encype = "multipart/form-data" > <input type="file" id="test" id="j_imgfile"> </form> <div> <img src="" id="j_imgPic"> </div>
$('#j_imgfile').on('change',function(){
// 判断上传文件类型
var objFile = $('#j_imgfile').val();
var objType = objFile.substring(objFile.lastIndexOf(".")).toLowerCase();
var formData = new FormData(document.forms.namedItem("picForm"));
console.log(objType);
if(!(objType == '.jpg'||objType == '.png'))
{
alert("请上传jpg、png类型图片");
return false;
}else{
$.ajax({
type : 'post',
url : '/uploadUserImgPre',
data: formData ,
processData:false,
async:false,
cache: false,
contentType: false,
success:function(re){
re.imgSrc = re.imgSrc.replace('public','');
re.imgSrc = re.imgSrc.replace(/\\/g,'\/');
$('#j_imgPic').attr('src',re.imgSrc);
},
error:function(re){
console.log(re);
}
});
}
});
app.post('/uploadUserImgPre',routes.users.uploadUserImgPre);
exports.uploadUserImgPre = function(req, res, next) {
//生成multiparty对象,并配置上传目标路径
var form = new multiparty.Form({uploadDir: './public/files/images'});
form.parse(req, function(err, fields, files) {
var filesTmp = JSON.stringify(files);
if(err){
console.log('parse error: ' + err);
} else {
testJson = eval("(" + filesTmp+ ")");
console.log(testJson.fileField[0].path);
res.json({imgSrc:testJson.fileField[0].path})
console.log('rename ok');
}
});
}
app.use(express.static(path.join(__dirname, 'public')));
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有