layui檔案上傳進度條(類比)

來源:互聯網
上載者:User

標籤:圖片   數值   clear   xhr   利用   res   java   .text   進度   

1.修改上傳組件js(沒測)

81017471

https://www.cnblogs.com/youmingkuang/p/9183528.html

https://fly.layui.com/jie/19430/

 

1、upload.js 擴充

功能利用ajax的xhr屬性實現
該功能修改過modules中的upload.js檔案
功能具體實現:
在js檔案中添加監聽函數

    //建立監聽函數     var xhrOnProgress=function(fun) {        xhrOnProgress.onprogress = fun; //綁定監聽         //使用閉包實現監聽綁        return function() {            //通過$.ajaxSettings.xhr();獲得XMLHttpRequest對象            var xhr = $.ajaxSettings.xhr();             //判斷監聽函數是否為函數              if (typeof xhrOnProgress.onprogress !== ‘function‘)                   return xhr;               //如果有監聽函數並且xhr對象支援綁定時就把監聽函數綁定上去                if (xhrOnProgress.onprogress && xhr.upload) {                      xhr.upload.onprogress = xhrOnProgress.onprogress;                }                return xhr;         }     }

初始化上傳

//初始化上傳 upload.render({     elem: ‘上傳地址‘     ,url: path+‘/upload/uploadVideo.do‘     ,accept: ‘video‘    ,size: 512000    ,auto:false    ,xhr:xhrOnProgress    ,progress:function(value){//上傳進度回調 value進度值        element.progress(‘demo‘, value+‘%‘)//設定頁面進度條     }    ,bindAction:‘#uploadvideo‘     ,before: function(input){    //返回的參數item,即為當前的input DOM對象        console.log(‘檔案上傳中‘);    }    ,done: function(res){             //上傳成功           console.log(res)     } });

修改modules中upload.js檔案的ajax方法

 //提交檔案        $.ajax({          url: options.url          ,type: options.method          ,data: formData          ,contentType: false           ,processData: false          ,dataType: ‘json‘          ,xhr:options.xhr(function(e){//此處為新添加功能                var percent=Math.floor((e.loaded / e.total)*100);//計算百分比                options.progress(percent);//回調將數值返回           })          ,success: function(res){            successful++;            done(index, res);            allDone();          }          ,error: function(e){             console.log(e)            aborted++;            that.msg(‘請求上傳介面出現異常‘);            error(index);            allDone();          }        });

後台代碼:

        public ActionResult UploadFiles(HttpPostedFileBase fileNames)        {            string path = "";            //小於20M            if (fileNames.ContentLength > 0 && fileNames.ContentLength <= 120971520)            {                var fileName = Path.GetFileName(fileNames.FileName);                string q_FN = fileName.Substring(0, fileName.LastIndexOf("."));                string h_FN = fileName.Substring(fileName.LastIndexOf("."));                string NewFileName = q_FN + DateTime.Now.ToString("yyyyMMddHHmmss") + h_FN;                path = Path.Combine(Server.MapPath("/Uploadfile/"), NewFileName);                fileNames.SaveAs(path);                path = "/Uploadfile/" + NewFileName;                var relt = new { tc = path };                return Content(JsonConvert.SerializeObject(relt));            }            else            {                var relt = new { tc = "上傳檔案要小於20M" };                return Content(JsonConvert.SerializeObject(relt));            }        }

 

功能到此結束!!!


列子:

 

 

2.類比一個假的進度條

80784717

layui.use([‘upload‘,‘element‘,‘layer‘], function(){var upload = layui.upload,element = layui.element,layer = layui.layer;var timer;//定義一個計時器//選完檔案後不自動上傳upload.render({    elem: ‘#test8‘    ,url: ‘upload‘    ,async: false    ,method: ‘post‘    ,data: {        upgradeType: function(){    return $("input[name=‘upgradeType‘]:checked").val();//額外傳遞的參數        }    }    ,auto: false    ,accept: ‘file‘ //普通檔案    ,exts: ‘zip‘ //只允許上傳壓縮檔    ,field:‘uploadFile‘    ,bindAction: ‘#test9‘    ,choose: function(obj){//這裡的作用是截取上傳檔案名稱並顯示var uploadFileInput=$(".layui-upload-file").val();var uploadFileName=uploadFileInput.split("\\");$("#uploadName").text(uploadFileName[uploadFileName.length-1]);    }    ,before: function(obj){ //obj參數包含的資訊,跟choose回調完全一致layer.load(); //上傳loadingvar n = 0; timer = setInterval(function(){//按照時間隨機產生一個小於95的進度,具體數值可以自己調整    n = n + Math.random()*10|0;      if(n>95){n = 95;clearInterval(timer);    }    element.progress(‘demo‘, n+‘%‘);}, 50+Math.random()*100);    }    ,done: function(res){clearInterval(timer);element.progress(‘demo‘, ‘100%‘);//一成功就把進度條置為100%layer.closeAll(); layer.msg(‘上傳成功‘);}    ,error: function(index, upload){element.progress(‘demo‘, ‘0%‘);layer.closeAll(); //關閉所有層layer.msg(‘上傳更新包失敗‘);    }});});

  

參考:https://www.layui.com/doc/modules/upload.html

layui檔案上傳進度條(類比)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.