加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MVC 通过ajaxSubmit上传图片并显示

发布时间:2020-12-16 01:41:45 所属栏目:百科 来源:网络整理
导读:js代码functionsubmitform(){$("#form_upload").ajaxSubmit({success:showResponse});}functionshowResponse(responseText){if(responseText!=null){alert('上传成功!');}else{alert('操作失败!');}}$(function(){$("#upImg").on("change",function(){varf
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("上传异常!");

}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读