pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <legend id='ovu0n'><style id='ovu0n'><dir id='ovu0n'><q id='ovu0n'></q></dir></style></legend>

      <bdo id='ovu0n'></bdo><ul id='ovu0n'></ul>
    <i id='ovu0n'><tr id='ovu0n'><dt id='ovu0n'><q id='ovu0n'><span id='ovu0n'><b id='ovu0n'><form id='ovu0n'><ins id='ovu0n'></ins><ul id='ovu0n'></ul><sub id='ovu0n'></sub></form><legend id='ovu0n'></legend><bdo id='ovu0n'><pre id='ovu0n'><center id='ovu0n'></center></pre></bdo></b><th id='ovu0n'></th></span></q></dt></tr></i><div class="ay2ay2a" id='ovu0n'><tfoot id='ovu0n'></tfoot><dl id='ovu0n'><fieldset id='ovu0n'></fieldset></dl></div>
    <tfoot id='ovu0n'></tfoot>
  • <small id='ovu0n'></small><noframes id='ovu0n'>

        使用 XMLHttpRequest 上傳大文件時的進度條

        Progress bar while uploading large files with XMLHttpRequest(使用 XMLHttpRequest 上傳大文件時的進度條)

            • <small id='sftgY'></small><noframes id='sftgY'>

              • <bdo id='sftgY'></bdo><ul id='sftgY'></ul>
                <i id='sftgY'><tr id='sftgY'><dt id='sftgY'><q id='sftgY'><span id='sftgY'><b id='sftgY'><form id='sftgY'><ins id='sftgY'></ins><ul id='sftgY'></ul><sub id='sftgY'></sub></form><legend id='sftgY'></legend><bdo id='sftgY'><pre id='sftgY'><center id='sftgY'></center></pre></bdo></b><th id='sftgY'></th></span></q></dt></tr></i><div class="2aagiw0" id='sftgY'><tfoot id='sftgY'></tfoot><dl id='sftgY'><fieldset id='sftgY'></fieldset></dl></div>
                <tfoot id='sftgY'></tfoot>

                    <tbody id='sftgY'></tbody>
                1. <legend id='sftgY'><style id='sftgY'><dir id='sftgY'><q id='sftgY'></q></dir></style></legend>
                2. 本文介紹了使用 XMLHttpRequest 上傳大文件時的進度條的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試使用 XMLHttpRequest 和 file.slice 將一些大文件上傳到服務(wù)器.
                  我已經(jīng)在文檔和其他各種鏈接的幫助下做到了這一點.
                  由于上傳大文件是一項漫長的工作,我想為用戶提供一個進度條.
                  經(jīng)過更多閱讀后,我遇到了 示例,理論上,正是我需要的.
                  通過獲取示例代碼并使其適應(yīng)我的需求,我達到了

                  I am trying to upload some large files to the server using XMLHttpRequest and file.slice.
                  I've manage doing this with the help of documentations and other various links.
                  Since uploading large file is a lengthily job, i would like to provide the user with a progress bar.
                  After some more readings i've come across on an example that, theoretically, does exactly what i need.
                  By taking the sample code and adapting it to my needs i reached

                  var upload =
                  {
                  blobs: [],
                  pageName: '',
                  bytesPerChunk: 20 * 1024 * 1024,
                  currentChunk: 0,
                  loaded: 0,
                  total: 0,
                  file: null,
                  fileName: "",
                  
                  uploadChunk: function (blob, fileName, fileType) {
                      var xhr = new XMLHttpRequest();
                  
                      xhr.onreadystatechange = function () {
                          if (xhr.readyState == 4) {
                              if (xhr.responseText) {
                                  // alert(xhr.responseText);
                              }
                          }
                      };
                  
                      xhr.addEventListener("load", function (evt) {
                          $("#dvProgressPrcent").html("100%");
                          $get('dvProgress').style.width = '100%';
                      }, false);
                  
                      xhr.addEventListener("progress", function (evt) {
                          if (evt.lengthComputable) {
                              var progress = Math.ceil(((upload.loaded + evt.loaded) / upload.total) * 100);
                              $("#dvProgressPrcent").html(progress + "%");
                              $get('dvProgress').style.width = progress + '%';
                          }
                      }, false);
                  
                      xhr.upload.addEventListener("progress", function (evt) {
                          if (evt.lengthComputable) {
                              var progress = Math.ceil(((upload.loaded + evt.loaded) / upload.total) * 100);
                              $("#dvProgressPrcent").html(progress + "%");
                              $get('dvProgress').style.width = progress + '%';
                          }
                      }, false);
                  
                      xhr.open('POST', upload.pageName, false);
                  
                      xhr.setRequestHeader("Content-Type", "multipart/form-data");
                      xhr.setRequestHeader("X-File-Name", fileName);
                      xhr.setRequestHeader("X-File-Type", fileType);
                      xhr.send(blob);
                  },
                  upload: function (file) {
                      var start = 0;
                      var end = 0;
                      var size = file.size;
                  
                      var date = new Date();
                      upload.fileName = date.format("dd.MM.yyyy_HH.mm.ss") + "_" + file.name;
                  
                      upload.loaded = 0;
                      upload.total = file.size;
                  
                      while (start < size) {
                          end = start + upload.bytesPerChunk;
                          if (end > size) {
                              end = size;
                          }
                  
                          var blob = file.slice(start, end);
                          upload.uploadChunk(blob, upload.fileName, file.type);
                          start = end;
                          upload.loaded += start;
                      }
                  
                      return upload.fileName;
                  }
                  };
                  

                  電話就像(沒有驗證)

                  upload.upload(document.getElementById("#upload").files[0]);
                  

                  我的問題是進度事件沒有觸發(fā).
                  我已經(jīng)為進度事件嘗試了 xhr.addEventListener 和 xhr.upload.addEventListener (一次和一次),但它永遠不會觸發(fā).onreadystatechange 和 load 事件觸發(fā)就好了.

                  My problem is that the progress event doesn't trigger.
                  I've tried xhr.addEventListener and with xhr.upload.addEventListener (each at a time and both at a time) for the progress event but it never triggers. The onreadystatechange and load events trigger just fine.

                  如果我做錯了什么,我將不勝感激

                  I would greatly appreciate help with what i am doing wrong

                  更新
                  經(jīng)過多次嘗試,我設(shè)法模擬了一個進度,但我遇到了另一個問題:Chrome 的 UI 在上傳期間沒有更新.
                  現(xiàn)在的代碼是這樣的

                  Update
                  After many attempts i've manage to simulate a progress but i've ran into another problem: Chrome's UI is not updating during the upload.
                  The code looks like this now

                  var upload =
                  {
                      pageName: '',
                      bytesPerChunk: 20 * 1024 * 1024,
                      loaded: 0,
                      total: 0,
                      file: null,
                      fileName: "",
                  
                      uploadFile: function () {
                          var size = upload.file.size;
                  
                          if (upload.loaded > size) return;
                  
                          var end = upload.loaded + upload.bytesPerChunk;
                          if (end > size) { end = size; }
                  
                          var blob = upload.file.slice(upload.loaded, end);
                  
                          var xhr = new XMLHttpRequest();
                  
                          xhr.open('POST', upload.pageName, false);
                  
                          xhr.setRequestHeader("Content-Type", "multipart/form-data");
                          xhr.setRequestHeader("X-File-Name", upload.fileName);
                          xhr.setRequestHeader("X-File-Type", upload.file.type);
                  
                          xhr.send(blob);
                  
                          upload.loaded += upload.bytesPerChunk;
                  
                          setTimeout(upload.updateProgress, 100);
                          setTimeout(upload.uploadFile, 100);
                      },
                      upload: function (file) {
                          upload.file = file;
                  
                          var date = new Date();
                          upload.fileName = date.format("dd.MM.yyyy_HH.mm.ss") + "_" + file.name;
                  
                          upload.loaded = 0;
                          upload.total = file.size;
                  
                          setTimeout(upload.uploadFile, 100);
                  
                  
                          return upload.fileName;
                      },
                      updateProgress: function () {
                          var progress = Math.ceil(((upload.loaded) / upload.total) * 100);
                          if (progress > 100) progress = 100;
                  
                          $("#dvProgressPrcent").html(progress + "%");
                          $get('dvProgress').style.width = progress + '%';
                      }
                  };
                  


                  更新 2
                  我已經(jīng)設(shè)法修復(fù)它并模擬了一個也適用于 chrome 的進度條.
                  我已經(jīng)用有效的代碼示例更新了之前的代碼示例.
                  您可以通過減小一次上傳的塊的大小來更頻繁地刷新"欄謝謝你的幫助


                  Update 2
                  I've managed to fix it and simulate a progress bar that works in chrome too.
                  i've updated previous code sample with the one that works.
                  You can make the bar 'refresh' more often by reducing the size of the chunk uploaded at a time Tahnk you for your help

                  推薦答案

                  如 https://stackoverflow.com/a/中所述3694435/460368,你可以這樣做:

                  if(xhr.upload)
                       xhr.upload.onprogress=upload.updateProgress;
                  

                  updateProgress: function updateProgress(evt) 
                  {
                     if (evt.lengthComputable) {
                         var progress = Math.ceil(((upload.loaded + evt.loaded) / upload.total) * 100);
                         $("#dvProgressPrcent").html(progress + "%");
                         $get('dvProgress').style.width = progress + '%';
                     }
                  }
                  

                  這篇關(guān)于使用 XMLHttpRequest 上傳大文件時的進度條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)

                      <small id='FL0Wt'></small><noframes id='FL0Wt'>

                      <i id='FL0Wt'><tr id='FL0Wt'><dt id='FL0Wt'><q id='FL0Wt'><span id='FL0Wt'><b id='FL0Wt'><form id='FL0Wt'><ins id='FL0Wt'></ins><ul id='FL0Wt'></ul><sub id='FL0Wt'></sub></form><legend id='FL0Wt'></legend><bdo id='FL0Wt'><pre id='FL0Wt'><center id='FL0Wt'></center></pre></bdo></b><th id='FL0Wt'></th></span></q></dt></tr></i><div class="00ug2gm" id='FL0Wt'><tfoot id='FL0Wt'></tfoot><dl id='FL0Wt'><fieldset id='FL0Wt'></fieldset></dl></div>

                          <tbody id='FL0Wt'></tbody>
                      • <tfoot id='FL0Wt'></tfoot>

                          <legend id='FL0Wt'><style id='FL0Wt'><dir id='FL0Wt'><q id='FL0Wt'></q></dir></style></legend>
                            <bdo id='FL0Wt'></bdo><ul id='FL0Wt'></ul>
                          • 主站蜘蛛池模板: 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 标准品网_标准品信息网_【中检计量】 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 篷房|仓储篷房|铝合金篷房|体育篷房|篷房厂家-华烨建筑科技官网 知名电动蝶阀,电动球阀,气动蝶阀,气动球阀生产厂家|价格透明-【固菲阀门官网】 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 瑞典Blueair空气净化器租赁服务中心-专注新装修办公室除醛去异味服务! | 云阳人才网_云阳招聘网_云阳人才市场_云阳人事人才网_云阳人家招聘网_云阳最新招聘信息 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 劳动法网-专业的劳动法和劳动争议仲裁服务网 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 压装机-卧式轴承轮轴数控伺服压装机厂家[铭泽机械] | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 | 汕头市盛大文化传播有限公司,www.11400.cc | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 皮带机-带式输送机价格-固定式胶带机生产厂家-河南坤威机械 | 北京成考网-北京成人高考网 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 结晶点测定仪-润滑脂滴点测定仪-大连煜烁 | 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 传爱自考网_传爱自学考试网 | 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 河南15年专业网站建设制作设计,做网站就找郑州启凡网络公司 | 浙江富广阀门有限公司 | 收录网| 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 |