mvc框架上传文件-1
来源:互联网 发布:威廉·密里根 知乎 编辑:程序博客网 时间:2024/05/22 15:19
前段时间在mvc框架中上传文件,今天整理做一下记录。 HTML:
<div id="upFiles" <div> <form id="uploadForm" name="uploadForm" action='@Url.Action("UploadFile", "Receipt")' method="post" enctype="multipart/form-data"> <input type="file" onchange="previewImage()" id="fileup" name="file" /> <input id="ButtonUpload" style="display:none;" type="submit" value="上传" /> </form> </div> <div class="fl"> <img style="width: 220px; height: 180px; display: none;" id="imghead2" class="imghead2" /> </div> <div class="fr apply-con-c5"> <img style="width: 220px; height: 180px; display: none;" id="imgBase" /> <input type="button" id="submit" onclick="submitApply();" /> </div> </div>
jQuery :
$("#submit").bind('click', function () { $("#common_message_receive").html("确认提交"); $("#common_warm_receive").show(); $(".masking").show(); $('#common_ok_receive').unbind('click').bind('click', function () { $('#common_warm_receive').hide(); $(".masking").hide(); var num = $("#imgBase").attr('src'); if (num != undefined) { var reqID = $("#reqID").textbox("getValue"); var annexName = $("#fileup").val(); var point = annexName.lastIndexOf("\\"); annexName = annexName.substr(point + 1); num = $('#imgBase').attr('src').indexOf("base64") + 7; var annexs = $('#imgBase').attr('src').substring(num); ehr.ajax(ehr.servicesUrl + 'Receipt/AddPageAnnex', { "reqID": reqID, "annexName": annexName, "annexs": annexs }, function (json) { if (json.Success) { $("#tipMessage3").html("提交成功"); $("#tip3").show(); $(".masking").show(); } else { $("#tipMessage4").html(json.Message); $("#tip4").show(); $(".masking").show(); } }); } else { $("#tipMessage4").html("未上传文件"); $("#tip4").show(); $(".masking").show(); } }); }); //图片上传预览 IE是用了滤镜。 function previewImage(file) { $("#uploadForm").submit(); } $("#uploadForm").ajaxForm({ iframe: true, dataType: "json", beforeSubmit: function () { if ($('#fileup').val() == '') { $("#tipMessage3").html("@ldt.Rows[29][2]") $("#tip3 .tip-con1").attr("src","@Url.Content("~/Content1/img/5.png")"); $("#tip3").show(); $(".masking").show(); return false; } }, success: function (result) { if (!result.Success) { $("#tip3 .tip-con1").attr("src", "@Url.Content("~/Content1/img/5.png")"); $("#tipMessage3").html("@ldt.Rows[30][2]"); $("#tip3").show(); $(".masking").show(); } else { $("#imgBase").attr("src", "data:image/png;base64," + result.Message); var filehtml = $("#fileup").val(); var point = filehtml.lastIndexOf("."); var type = filehtml.substr(point); showImage(type); } }, error: function (xhr, textStatus, errorThrown) { $("#tipMessage3").html("@ldt.Rows[45][2]"); $("#tip3 .tip-con1").attr("src","@Url.Content("~/Content1/img/5.png")"); $("#tip3").show(); $(".masking").show(); } });
记得要引用jquery.form.js文件
后台代码:
public string UploadFile(HttpPostedFileBase file) { if (file == null || file.ContentLength == 0) { return ConvertToJson(new { Code = -1, Success = false, Message = "没有任何文件!" }); } string path = Request.MapPath("~/Upload"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName)); var newPath = Url.Content("~/Upload/" + Path.GetFileName(file.FileName)); try { string file_type = file.ContentType; file.SaveAs(fileName); string picUrl = ImgToBase64String(fileName, file_type); if (!string.IsNullOrEmpty(picUrl)) { file.InputStream.Close(); file.InputStream.Dispose(); return ConvertToJson(new { Code = 1, Success = true, Message = picUrl }); } file.InputStream.Close(); file.InputStream.Dispose(); return ConvertToJson(new { Code = -1, Success = false, Message = "上传失败!" }); } catch { file.InputStream.Close(); file.InputStream.Dispose(); return ConvertToJson(new { Code = -1, Success = false, Message = "上传失败!" }); } finally { file.InputStream.Close(); file.InputStream.Dispose(); } }
public ActionResult AddPageAnnex(string reqID, string annexName, string annexs) { JsonRult result = new JsonRult(); string empid = Session["uID"] == null ? "" : Session["uID"].ToString(); if (empid == "") { result.Message = "请先登录"; result.Success = false; result.Value = ""; var rultResult = Json(result, JsonRequestBehavior.AllowGet); return rultResult; } else { EHR.EhrWebService.Android_ServiceSoapClient cli = new EhrWebService.Android_ServiceSoapClient(); string sMsg = null; var item = cli.AddPageAnnex(reqID, annexName, annexs, ref sMsg); result.Message = sMsg; result.Success = (item == 0 ? true : false); var rultResult = Json(result, JsonRequestBehavior.AllowGet); return rultResult; } }
显示效果:
阅读全文
0 0
- mvc框架上传文件-1
- mvc框架上传文件-2
- 使用spring MVC框架进行文件上传
- 使用spring MVC框架进行文件上传
- 使用spring MVC框架进行文件上传
- 使用spring MVC框架进行文件上传
- 使用spring MVC框架进行文件上传
- 文件上传--基于Spring MVC框架+SmartUpload
- Spring-Mvc框架实现文件上传
- HAP框架+spring MVC下上传文件
- Spring mvc框架的web上传文件文档
- 从零实现MVC框架之文件上传(8)
- MVC+WCF框架下广告位管理——文件上传
- Java String MVC框架使用uploadify上传文件
- Spring Web MVC框架(五) 文件上传
- spring mvc +uploadify2.1 实现文件上传
- uploadify 3.2.1+spring mvc上传文件
- spring mvc multipart文件上传-1
- Android常用的动画和小型代码理解
- ssh整合测试
- 一串数字分区间显示
- VS SDK更新问题(error MSB8036: 找不到 Windows SDK 版本10.0.14393.0)
- MATLAB中处理边界的函数
- mvc框架上传文件-1
- Java基础(二)运算符
- 数字黑洞 (20)
- 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url
- myeclipse->project->clean…到底有什么用
- ionic2中的LoadingController用法。在数据加载完之前显示
- JQ模态窗口自适应
- (闲来无事)栈的实现
- 为什么要用nodejs当做前台 java当做后台