首先引入jquery.form.js
1.创建一个form表单(("~/Content/productPic/")是保存图片的路径)
@using(Html.BeginForm("UploadIcon","FlowChartForm",FormMethod.Post,new {ID="uploadpic",enctype="multipart/form-data"}))
{
<input type="file" name="imgfile" οnchange="$('#uploadpic').submit();" style="width:200px;"/><br/>
<img id="imgicon" src="@(Url.Content("~/Content/productPic/")+ViewData["picName"])" style="display:none;width:80px;height:60px;"/>
@Html.Hidden("iconurl",ViewData["picName"]);
}
2.使用ajax提交图片(进行ajaxt提交的方法)
$(function(){
$('uploadpic').ajaxForm({
success:function(data){
var data=jQuery.parseJSON(data);
if(data!=undefined&&data!=null){
if(data.msg==0){
alert("请上传图片!");
return;
}else if(data.msg==-1){
alert("文件格式不正确!");
return;
}else if(data.mag==-2){
alert("上传图片不能超过3M!");
return;
}else if(data.msg==-3){
alert("出现异常,请稍后再试")!\
return;
}else{
//如果你上传到的是本地可以这样写
//var path="@Url.Content("../Content/productPic")"+data.msg;
//$("#imgicon").attr("src",path);
//$("#imgicon").show();
downpic(data.msg);
}
}
}
})
});
function downpic(msg){