mvc框架上传文件-1

5 篇文章 0 订阅
5 篇文章 0 订阅
 前段时间在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;
            }
        }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值