layui.load 批量上传文件在H5中的使用


//页面JS

   layui.use('upload', function () {

                var $ = layui.jquery,
                    upload = layui.upload;


                //  多文件列表示例
                var demoListView = $('#demoList')
                , uploadListIns = upload.render({
                    elem: '#testList'


                  , url: '/api/UploadMullFiles/getTest2'
                     , method: 'post'
                  , accept: 'file'
                  , multiple: true
                  , auto: false


                  , bindAction: '#testListAction'
                  , choose: function (obj) {
                      var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                      //读取本地文件
                      obj.preview(function (index, file, result) {
                          var tr = $(['<tr id="upload-' + index + '">'
                            , '<td>' + file.name + '</td>'
                            , '<td>' + '张三' + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                          , '<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                          , '<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                      , '</tr>'].join(''));


                          //单个重传
                          tr.find('.demo-reload').on('click', function () {
                              obj.upload(index, file);
                          });


                          //删除
                          tr.find('.demo-delete').on('click', function () {
                              delete files[index]; //删除对应的文件
                              tr.remove();
                              uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
                          });


                          demoListView.append(tr);
                      });
                  }
                , done: function (res, index, upload) {


                    debugger;




                    if (res[0].JOB_NAME == "上传成功!") { //上传成功
                        var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                        upname = upname + "$" + res[0].ORG_NAME;
                        uptitle = uptitle + "$" + res[0].B_NAME
                        tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                        tds.eq(3).html(''); //清空操作


                        return delete this.files[index]; //删除文件队列已经上传成功的文件
                    }
                    this.error(index, upload);
                }
                , error: function (index, upload) {
                    var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                    tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                    tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                }
                });




            });


因为是在H5中使用所以他的跳转接口必须为控制器

 /// <summary>
        /// 通用上传附件信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string getTest2()
        {


            //string id = HttpContext.Current.Request["id"];
            //string name = HttpContext.Current.Request["name"];
            HttpFileCollection files = HttpContext.Current.Request.Files;


            Random ran = new Random();




            string jsonSSS = "";


            string uploadPath = "C:\\ftp\\UploadFiles\\";
            foreach (string key in files.AllKeys)
            {
                HttpPostedFile file = files[key];//file.ContentLength文件长度  
                //if (string.IsNullOrEmpty(file.FileName) == false)
                //    file.SaveAs(HttpContext.Current.Server.MapPath(uploadPath) + file.FileName);




                //return Ok("success2");


                var filecombin = file.FileName.Split('.');


                var tmp = file.FileName;
                var tmpIndex = 0;


                string FILEXX = "";


                //判断是否存在相同文件名的文件 相同累加1继续判断  
                while (System.IO.File.Exists(uploadPath + tmp))
                {
                    tmp = filecombin[0] + "_" + tmpIndex + "." + filecombin[1];


                    FILEXX = filecombin[1];


                }
                //不带路径的最终文件名  
                //   FILEXX = tmp;






                string RandKey = GETRandKey();


                System.Threading.Thread.Sleep(100);  //当前线程休息0.1秒






                string name = "";
                string msg = "";
                string URL = "";
                if (file != null)
                {
                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }


                    System.Threading.Thread.Sleep(100);  //当前线程休息0.1秒
                    string newFiles = DateTime.Now.ToString("yyMMddHHmmss") + ran.Next(1000, 9999).ToString() + "." + filecombin[1];
                    file.SaveAs(uploadPath + newFiles);








                    List<model.HHData.USERmESSAGE> lis = new List<model.HHData.USERmESSAGE>();




                    model.HHData.USERmESSAGE us = new model.HHData.USERmESSAGE();


                    us.B_NAME = file.FileName;
                    us.JOB_NAME = "上传成功!";
                    us.ORG_NAME = "ftp:\\\\888.88.88.888:8888\\UploadFiles\\" + newFiles;
                 //FTP  后面为IP及端口  这个根据需要更改
                    lis.Add(us);


                    jsonSSS = JsonConvert.SerializeObject(lis);






                    //  context.Response.Write(jsonSSS);










                    //HttpContext.Current.Response.ContentType = "application/json";
                    //HttpContext.Current.Response.Charset = "utf-8";






                    //HttpContext.Current.Response.Write(jsonSSS);








                    //HttpContext context = HttpContext.Current;


                    //context.Response.Write(jsonSSS);




                    return jsonSSS;


                }




            }


            return jsonSSS;


            //return Ok(jsonSSS);
        }


        /// <summary>
        /// 获取四位随机数
        /// </summary>
        /// <returns></returns>
        public string GETRandKey()
        {


            Random ran = new Random();
            int RandKey = ran.Next(1000, 9999);


            return RandKey.ToString();

        }



这个方法中

     HttpFileCollection files = HttpContext.Current.Request.Files;    这句最重要,因为这个是获取页面的上传控件信息。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于多个文件上传和提交同时进行的控制,可以使用 layui.upload 的 before 函数来实现。 示例代码如下: ``` layui.use('upload', function(){ var upload = layui.upload; //多文件列表示例 var demoListView = $('#demoList'), uploadListIns = upload.render({ elem: '#testList', url: '/upload/', accept: 'file', multiple: true, auto: false, bindAction: '#testListAction', choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列 //读取本地文件 obj.preview(function(index, file, result){ var tr = $(['<tr id="upload-'+ index +'">', '<td>'+ file.name +'</td>', '<td>'+ (file.size/1014).toFixed(1) +'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join('')); //单个重传 tr.find('.demo-reload').on('click', function(){ obj.upload(index, file); }); //删除 tr.find('.demo-delete').on('click', function(){ delete files[index]; //删除对应的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 }); demoListView.append(tr); }); }, before: function(obj){ layer.load(); //上传loading //模拟loading setTimeout(function(){ layer.closeAll('loading'); }, 2000); }, done: function(res, index, upload){ if(res.code == 0){ //上传成功 var tr = demoListView.find('tr#upload-'+ index), tds = tr.children(); tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); tds.eq(3).html(''); //清空操作 return delete this.files[index]; //删除文件队列已经上传成功的文件 } this.error(index, upload); }, error: function(index, upload){ var tr = demoListView.find('tr#upload-'+ index), tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传 } }); }); ``` 其,before 函数用于上传之前的处理,这里可以添加 loading 等提示信息,确保上传过程用户可以看到正在处理。 另外,由于 layui.upload 是基于 layui.form 实现的,所以在页面需要有一个 form 标签,同时需要设置一个 bindAction 参数指向提交按钮,这样在上传完成后才能触发表单提交。 希望这个示例代码能够帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值