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>
                          • 主站蜘蛛池模板: 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 潍坊大集网-潍坊信息港-潍坊信息网 | PVC快速门-硬质快速门-洁净室快速门品牌厂家-苏州西朗门业 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 合肥钣金加工-安徽激光切割加工-机箱机柜加工厂家-合肥通快 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 武汉创亿电气设备有限公司_电力检测设备生产厂家 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 三板富 | 专注于新三板的第一垂直服务平台 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 实验室隔膜泵-无油防腐蚀隔膜泵-耐腐蚀隔膜真空泵-杭州景程仪器 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 上海律师事务所_上海刑事律师免费咨询平台-煊宏律师事务所 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 电缆接头-防爆电缆接头-格兰头-金属电缆接头-防爆填料函 | 技德应用| 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 合肥升降机-合肥升降货梯-安徽升降平台「厂家直销」-安徽鼎升自动化科技有限公司 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 低压载波电能表-单相导轨式电能表-华邦电力科技股份有限公司-智能物联网综合管理平台 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 浙江美尔凯特智能厨卫股份有限公司 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 |