转载 layui upload 上传文件 怎么传入动态参数

原文地址: https://blog.csdn.net/luobowangjing/article/details/141651198

主要是:

 , before: function (obj) {
        this.data = {
            quarter: $("#selExport").val()
        }

        $(".layui-progress").show();
        element.progress('upload_progress', '0%'); //进度条复位
        layer.msg('上传中', { icon: 16, time: 0 });
    }   

举例子:

<select class="my-select" id="txtProject" ></select>
<select class="my-select" id="txtType" ></select>

<label><input type="radio" name="Same" value="新增" checked>&nbsp;新增</label>
<label><input type="radio" name="Same" value="覆盖">&nbsp;覆盖</label>

<textarea style="width:200px; height:100px; resize: none;"></textarea>

<input type="file" id="file" name="file" style="display:none;" />
<button type="button" class="layui-btn" id="btn_Upload" >选择文件并上传</button>

  <script type="text/javascript">
      layui.use(['table', 'layer', 'upload'],
          function () {
              var table = layui.table,
                  $ = layui.$,
                  upload = layui.upload,
                  layer = layui.layer;

              //初始化文件上传
              upload.render({
                  elem: '#btn_Upload',//上传按钮的id
                  field: 'file',//file控件的id
                  url: '/PM/Uplaod',//ajax的url
                  accept: 'file', // 允许上传的文件类型
                  before: function (obj) {//往后台传递参数
                      this.data = {//类似ajax的data:{}
                          "ProjectID": $("#txtProject").val(),
                          "ProjectName": $("#txtProject").find("option:selected").text(),
                          "Type": $("#txtType").val(),
                          "Remark": $("#txtRemark").val(),
                          "Same": $('input[name="Same"]:checked').val(),
                      }
                  },
                  done: function (res) {
                      if (res.success) {
                          layer.msg(res.message, {
                              icon: 1,
                              time: 1000 //1秒关闭(如果不配置,默认是3秒)
                          }, function () {
                              parent.location.reload(); //刷新父页面
                          });
                      }
                      else {
                          layer.msg(res.message);
                      }
                  },
                  error: function () {
                      layer.alert('上传失败', { icon: 2 });
                  }
              });
          });
  </script>

 /// <summary>
 /// 上传附件
 /// </summary>
 public ActionResult Uplaod(int ProjectID, string ProjectName, string Type, string Remark, string Same)
 {
     MyResponse result = new MyResponse();

     HttpFileCollectionBase files = Request.Files;//接收文件
     if (!string.IsNullOrEmpty(files[0].FileName))//判断文件是否存在
     {
         try
         {
             string fileName = System.IO.Path.GetFileName(files[0].FileName);//文件名
             string extName = System.IO.Path.GetExtension(fileName).Replace(".", "");//扩展名,它前面包括一个点:.
             fileName = fileName.Replace(extName, "").Replace(".", "");//去掉扩展名

             string newName = $"{Type}_{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.{extName}";//新文件名

             newName = newName.Replace(" ", "");//去掉文件名中的空格

             string url = $"upload\\PMP\\{ProjectID}";//相对路径
             string serverDir = $"{Server.MapPath("/")}{url}";//服务器上的物理路径
             if (Directory.Exists(serverDir) == false)//物理路径不存在
             {
                 Directory.CreateDirectory(serverDir);//创建物理路径
             }

             string fullPath = $"{serverDir}\\{newName}";//服务器上的路径+文件名
             files[0].SaveAs(fullPath);//写入到服务器
             Thread.Sleep(1000);//给文件上传的时间

             if (System.IO.File.Exists(fullPath) == true)//判断文件是否已上传
             {
                 PMP_Attachment att = new PMP_Attachment();//附件实体
                 att.ProjectID = ProjectID;//项目id
                 att.ProjectName = ProjectName;//项目名称
                 att.AttachmentType = Type;//附件类型
                 att.AttachmentName = fileName.Replace(" ", "");//客户端上传的本地文件名
                 att.Extension = extName;//扩展名
                 att.FileName = newName.Replace(" ", "");//保存在服务器上的文件名
                 att.Url = $"{url}\\{newName}".Replace("\\", "/").Replace(" ", "");//相对路径+新文件名
                 att.Remark = Remark;//备注
                 att.IsDelete = false;
                 att.CreateBy = WorkContext.UserName;
                 att.CreateTime = DateTime.Now;

                 bool b = pmBll.AttachmentUplaod(att, Same);
                 result = DefaultResult(result, b, "上传");
             }
         }
         catch (Exception ex)
         {
             result.success = false;
             result.message = $"上传文件出错!{ex.Message}";
         }
     }
     else
     {
         result.success = false;
         result.message = "上传文件出错!请选择要上传的文件!";
     }
     return Json(result);
 }

猜你喜欢

转载自blog.csdn.net/djk8888/article/details/143482768