MVC 通过ajaxSubmit上传图片并显示

js代码
functionsubmitform(){
$("#form_upload").ajaxSubmit({
success:showResponse
});
}

functionshowResponse(responseText){
if(responseText!=null){
alert('上传成功!');
}else{
alert('操作失败!');
}
}

$(function(){
$("#upImg").on("change",function(){
varfile=this.files[0];

if(this.files&&file){
varreader=newFileReader();
reader.onload=function(e){
$('#result').attr('src',e.target.result);
}
reader.readAsDataURL(file);
}
});
})
前台代码
<table>
<tr>
<tdstyle="padding-top:20px;">生产(经营)许可证证件照片</td>
<td>
<formid="form_upload"style="height:4px;"action="Upload"target="iframeInfo"method="post"enctype="multipart/form-data">
<inputname="upImg"id="upImg"type="file"/>
<inputtype="submit"value="上传"/>
</form>
</td>
</tr>
<tr>
<td>
<imgid="result"style="width:200px;height:200px;"src=""alt="">
</td>
<td>
<iframename="iframeInfo"id="iframeInfo"style="border:0px;"></iframe>
</td>
</tr>
</table>

(这里添加iframe,因为后台返回时会跳转,把form放入iframe里提交就不会跳转页面)
后台代码
[HttpPost]
publicActionResultUpload(HttpPostedFileBaseupImg)
{
if(upImg==null)
{
returnContent("文件上传错误,请重新选择文件!");

}
stringfileName=System.IO.Path.GetFileName(upImg.FileName);
stringfilePhysicalPath=Server.MapPath("~/credimages/"+fileName);
try
{
upImg.SaveAs(filePhysicalPath);
Session["ImgPath"]=filePhysicalPath;
returnContent("上传成功");
}
catch
{
returnContent("上传异常!");

}
}

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...