asp.net MVC+easyUI 非form表单提交上传文件

7 篇文章 0 订阅
4 篇文章 0 订阅

不同于上次文件上传,本次上传在js中提交,且上传后重命名文件并存名与数据库
HTML代码

    <div>
        <form id="form" name="form" method="post" enctype="multipart/form-data" >
            <div class="container_16" style="width:90%;margin:5%;">
                <div class="grid_3 lbl">选择员工</div>
                <div>
                    <input id="txtFullName" name="FullName" class="easyui-combogrid" style="width: 220px" />
                    <input id="txtEmpId" name="EmpId" class="z-txt readonly" readonly="readonly" style="width: 200px" type="hidden" />
                </div>
                <div class="clear"></div>
                <div class="grid_3 lbl">选择文件</div>
                <div class="grid_13 val">
                    <input type="file" class="easyui-filebox" id="txtUpload"multiple="multiple" name="fileImport" style="width:260px;">
                    <a id="uploadFile" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="#">上传</a>
                </div>
            </div>
        </form>
        <div style="text-align:center;margin-top:15px">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" id="txtSure" data-bind="click:confirmClick" href="javascript:void(0)">保存</a>
            <a class="easyui-linkbutton" id="btnCancel" data-options="iconCls:'icon-cancel'" data-bind="click:cancelClick" href="javascript:void(0)">取消</a>
        </div>
        <div class="clear"></div>       
        <p align="center" ><font  color="#B22222">注:为避免重名,系统会自动重命名文件</font></p>
    </div>

js代码

//点击上传按钮
 $("#uploadFile").bind('click', function (e) {
     var file = parent.$("#txtUpload").prop('files');
     var size = 0;
     if (file.length == 0) {
         com.message('warning', '请选择文件!');
         return false;
      } else {
         for (var i = 0; i < file.length; i++) {
             size += file[i].size;
          }
     	if (size / 1024 / 1024 > 25) {
         com.message('warning', '文件过大,请上传小于25M的文件!');
         return false;
     	 }
  
         for (var i = 0; i < file.length; i++) {
            var l = file[i].name.split(".").length;                    
            var type = file[i].name.split(".")[l - 1];
            if (type == "doc" || type == "docx" || type == "xls" || type == "xlsx" || type == "ppt" || type == "pptx" || type == "pdf" || type == "jpg" || type == "bmp" || type == "gif" || type == "png" || type == "txt" || type == "zip" || type == "rar") {                                              
               } else {
                   $.messager.alert('提示', ' 暂不支持上传' + type + '文件,请重新选择!', 'info');
                   return false;
                     }
                 }
          //核心!!!
          var formData = new FormData($("#form")[0]);           
          $.ajax({
             url: "/api/base/ContractFile/PostData",
             type: 'POST',
             data: formData,
             async: false,
             cache: false,
             contentType: false,
             processData: false,
             success: function (d) {       
             //将后台返回的数据存于缓存,通过window.localStorage.getItem("ContractFileName");获取d                                                      
             	window.localStorage.setItem("ContractFileName", d);
                $.messager.alert('提示', '上传成功,记得点下方保存哦', 'info');
                    },
             error: function (d) {
                 $.messager.alert('提示', '上传失败', 'info');
                       }
                  });
         }           
  });

后台代码

 [System.Web.Http.HttpPost]
 public List<dynamic> PostData()
 {
   string pathForSaving = HttpContext.Current.Server.MapPath(string.Format("~/{0}", @"Temp\Contract"));      
   HttpFileCollection files = HttpContext.Current.Request.Files;
   string tempName = "";
   string path="";
   dynamic result = new ExpandoObject();
   List<dynamic> dt = new List<dynamic>();
   for (int i = 0; i < files.Count; i++)
   {
      string type = files[i].FileName.Substring(files[i].FileName.LastIndexOf(".") + 1);
      if (type == "doc" || type == "docx" || type == "xls" || type == "xlsx" || type == "ppt" || type == "pptx" || type == "pdf" || type == "jpg" || type == "bmp" || type == "gif" || type == "png" || type == "txt" || type == "zip" || type == "rar")
          {
              tempName = Path.GetFileNameWithoutExtension(files[i].FileName) + DateTime.Now.ToString("_yyyyMMddHHmmssfff") + Path.GetExtension(files[i].FileName);
               path = Path.Combine(pathForSaving, tempName);
               try
                {
                        files[i].SaveAs(path);
                        result.msg = "success";
                        dt.Add(tempName);
                    }
                catch (Exception)
                    {
                        result.msg = "error";
                    }
                }
            }           
            return dt;
    }

在这里插入图片描述
上面只贴出了上传按钮实现代码,保存按钮用于将上传后的文件名及对应员工存于数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值