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;            }        }

显示效果:这里写图片描述

原创粉丝点击