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

  • <small id='4JVa3'></small><noframes id='4JVa3'>

    <tfoot id='4JVa3'></tfoot>

      <bdo id='4JVa3'></bdo><ul id='4JVa3'></ul>

      <i id='4JVa3'><tr id='4JVa3'><dt id='4JVa3'><q id='4JVa3'><span id='4JVa3'><b id='4JVa3'><form id='4JVa3'><ins id='4JVa3'></ins><ul id='4JVa3'></ul><sub id='4JVa3'></sub></form><legend id='4JVa3'></legend><bdo id='4JVa3'><pre id='4JVa3'><center id='4JVa3'></center></pre></bdo></b><th id='4JVa3'></th></span></q></dt></tr></i><div class="bf3hhoj" id='4JVa3'><tfoot id='4JVa3'></tfoot><dl id='4JVa3'><fieldset id='4JVa3'></fieldset></dl></div>
        <legend id='4JVa3'><style id='4JVa3'><dir id='4JVa3'><q id='4JVa3'></q></dir></style></legend>

        顯示使用 XHR2/AJAX 下載文件的進(jìn)度條

        Show a progress bar for downloading files using XHR2/AJAX(顯示使用 XHR2/AJAX 下載文件的進(jìn)度條)
          <bdo id='8rVTD'></bdo><ul id='8rVTD'></ul>
          <legend id='8rVTD'><style id='8rVTD'><dir id='8rVTD'><q id='8rVTD'></q></dir></style></legend>
          <i id='8rVTD'><tr id='8rVTD'><dt id='8rVTD'><q id='8rVTD'><span id='8rVTD'><b id='8rVTD'><form id='8rVTD'><ins id='8rVTD'></ins><ul id='8rVTD'></ul><sub id='8rVTD'></sub></form><legend id='8rVTD'></legend><bdo id='8rVTD'><pre id='8rVTD'><center id='8rVTD'></center></pre></bdo></b><th id='8rVTD'></th></span></q></dt></tr></i><div class="yajufbj" id='8rVTD'><tfoot id='8rVTD'></tfoot><dl id='8rVTD'><fieldset id='8rVTD'></fieldset></dl></div>

                <tfoot id='8rVTD'></tfoot>

                  <tbody id='8rVTD'></tbody>
              1. <small id='8rVTD'></small><noframes id='8rVTD'>

                  本文介紹了顯示使用 XHR2/AJAX 下載文件的進(jìn)度條的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試使用 Ajax 下載文件并顯示自定義下載進(jìn)度條.

                  問題是我不明白該怎么做.我編寫了代碼來記錄進(jìn)度,但不知道如何啟動(dòng)下載.

                  注意:文件屬于不同類型.

                  提前致謝.

                  JS

                  //文件下載filelist.on('click', '.download_link', function(e){e.preventDefault();var id = $(this).data('id');$(this).parent().addClass("download_start");$.ajax({xhr: 函數(shù) () {var xhr = 新窗口.XMLHttpRequest();//處理下載進(jìn)度xhr.addEventListener("進(jìn)度", function (evt) {如果(evt.lengthComputable){var percentComplete = evt.loaded/evt.total;console.log(percentComplete);}}, 錯(cuò)誤的);返回xhr;},完成:函數(shù)(){console.log("請求完成");}})});

                  HTML 和 PHP

                   <li><div class="f_icon"><img src="'.$ico_path.'"></div><div class="left_wing"><div class="progressbar"></div><a class="download_link" href="#" id="'.$file_id.'"><div class="f_name">'.$full_file_name .'</div></a><div class="f_time_size">'.日期(M d,Y",$file_upload_time).' &#149;&nbsp;'.human_filesize($file_size) .'</div></div><div class="right_wing"><div 類="f_delete"><a class="btn btn-danger" href="#" aria-label="Delete" data-id="'.$file_id.'" data-filename="'.$full_file_name.'"><;i class="fa fa-trash-o fa-lg" aria-hidden="true" title="刪除這個(gè)?"></i></a></div></div></li>

                  解決方案

                  如果您想向用戶顯示下載過程的進(jìn)度條 - 您必須在 xmlhttprequest 中進(jìn)行下載.這里的問題之一是,如果您的文件很大 - 它們將在瀏覽器將它們寫入磁盤之前保存在瀏覽器的內(nèi)存中(使用常規(guī)下載文件時(shí)直接保存到磁盤,這樣可以在大文件上節(jié)省大量內(nèi)存).

                  另一個(gè)需要注意的重要事項(xiàng) - 為了使 lengthComputable 為真 - 您的服務(wù)器必須發(fā)送帶有文件大小的 Content-Length 標(biāo)頭.

                  這里是javascript代碼:

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="a1" data-filename="filename.xml">點(diǎn)擊下載</div><腳本>$('#a1').click(function() {var that = this;var page_url = 'download.php';var req = new XMLHttpRequest();req.open("POST", page_url, true);req.addEventListener("進(jìn)度", function (evt) {如果(evt.lengthComputable){var percentComplete = evt.loaded/evt.total;console.log(percentComplete);}}, 錯(cuò)誤的);req.responseType = "blob";req.onreadystatechange = function () {如果(req.readyState === 4 && req.status === 200){var filename = $(that).data('filename');if (typeof window.chrome !== 'undefined') {//鉻版本var link = document.createElement('a');link.href = window.URL.createObjectURL(req.response);link.download = 文件名;鏈接.click();} else if (typeof window.navigator.msSaveBlob !== 'undefined') {//IE版本var blob = new Blob([req.response], { type: 'application/force-download' });window.navigator.msSaveBlob(blob, 文件名);} 別的 {//火狐版本var file = new File([req.response], filename, { type: 'application/force-download' });window.open(URL.createObjectURL(file));}}};req.send();});</腳本>

                  下面是你可以使用的 php 代碼示例:

                  <塊引用>

                  請注意,我添加了一個(gè)睡眠來模擬慢速連接以在 localhost 上進(jìn)行測試.
                  您應(yīng)該在生產(chǎn)中刪除此 :)

                  I am trying to download files using Ajax and show a custom download progress bar.

                  The problem is I can't understand how to do so. I wrote the code to log the progress but don't know how to initiate the download.

                  NOTE: The files are of different types.

                  Thanks in advance.

                  JS

                  // Downloading of files
                  filelist.on('click', '.download_link', function(e){
                      e.preventDefault();
                      var id = $(this).data('id');
                      $(this).parent().addClass("download_start");
                  
                      $.ajax({
                          xhr: function () {
                              var xhr = new window.XMLHttpRequest();
                              // Handle Download Progress
                              xhr.addEventListener("progress", function (evt) {
                                  if(evt.lengthComputable) {
                                      var percentComplete = evt.loaded / evt.total;
                                      console.log(percentComplete);
                                  }
                              }, false);
                              return xhr;
                          },
                          complete: function () {
                              console.log("Request finished");
                          }
                      })
                  });
                  

                  HTML and PHP

                      <li>
                      <div class="f_icon"><img src="' . $ico_path . '"></div>
                      <div class="left_wing">
                         <div class="progressbar"></div>
                         <a class="download_link" href="#" id="'.$file_id.'"><div class="f_name">' . $full_file_name . '</div></a>
                         <div class="f_time_size">' . date("M d, Y", $file_upload_time) . '&nbsp; &#149; &nbsp;' . human_filesize($file_size) . '</div>
                      </div>
                  
                      <div class="right_wing">
                         <div class="f_delete">
                         <a class="btn btn-danger" href="#" aria-label="Delete" data-id="'.$file_id.'" data-filename="'.$full_file_name.'"><i class="fa fa-trash-o fa-lg" aria-hidden="true" title="Delete this?"></i>
                         </a>
                      </div>
                     </div>
                      </li>
                  

                  解決方案

                  If you want to show the user a progress-bar of the downloading process - you must do the download within the xmlhttprequest. One of the problems here is that if your files are big - they will be saved in the memory of the browser before the browser will write them to the disk (when using the regular download files are being saved directly to the disk, which saves a lot of memory on big files).

                  Another important thing to note - in order for the lengthComputable to be true - your server must send the Content-Length header with the size of the file.

                  Here is the javascript code:

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <div id="a1" data-filename="filename.xml">Click to download</div>
                  <script>
                  $('#a1').click(function() {
                      var that = this;
                      var page_url = 'download.php';
                  
                      var req = new XMLHttpRequest();
                      req.open("POST", page_url, true);
                      req.addEventListener("progress", function (evt) {
                          if(evt.lengthComputable) {
                              var percentComplete = evt.loaded / evt.total;
                              console.log(percentComplete);
                          }
                      }, false);
                  
                      req.responseType = "blob";
                      req.onreadystatechange = function () {
                          if (req.readyState === 4 && req.status === 200) {
                              var filename = $(that).data('filename');
                              if (typeof window.chrome !== 'undefined') {
                                  // Chrome version
                                  var link = document.createElement('a');
                                  link.href = window.URL.createObjectURL(req.response);
                                  link.download = filename;
                                  link.click();
                              } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                                  // IE version
                                  var blob = new Blob([req.response], { type: 'application/force-download' });
                                  window.navigator.msSaveBlob(blob, filename);
                              } else {
                                  // Firefox version
                                  var file = new File([req.response], filename, { type: 'application/force-download' });
                                  window.open(URL.createObjectURL(file));
                              }
                          }
                      };
                      req.send();
                  });
                  </script>
                  

                  And here is an example for the php code you can use:

                  <?php
                  $filename = "some-big-file";
                  $filesize = filesize($filename);
                  
                  header("Content-Transfer-Encoding: Binary");
                  header("Content-Length:". $filesize);
                  header("Content-Disposition: attachment");
                  
                  $handle = fopen($filename, "rb");
                  if (FALSE === $handle) {
                      exit("Failed to open stream to URL");
                  }
                  
                  while (!feof($handle)) {
                      echo fread($handle, 1024*1024*10);
                      sleep(3);
                  }
                  
                  fclose($handle);
                  

                  Note that I added a sleep to simulate a slow connection for testing on localhost.
                  You should remove this on production :)

                  這篇關(guān)于顯示使用 XHR2/AJAX 下載文件的進(jìn)度條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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) 之后,瀏覽器也會(huì)等待 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))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)

                    <legend id='oFHtX'><style id='oFHtX'><dir id='oFHtX'><q id='oFHtX'></q></dir></style></legend>

                      <tbody id='oFHtX'></tbody>

                        • <bdo id='oFHtX'></bdo><ul id='oFHtX'></ul>

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

                            <i id='oFHtX'><tr id='oFHtX'><dt id='oFHtX'><q id='oFHtX'><span id='oFHtX'><b id='oFHtX'><form id='oFHtX'><ins id='oFHtX'></ins><ul id='oFHtX'></ul><sub id='oFHtX'></sub></form><legend id='oFHtX'></legend><bdo id='oFHtX'><pre id='oFHtX'><center id='oFHtX'></center></pre></bdo></b><th id='oFHtX'></th></span></q></dt></tr></i><div class="qnawtdd" id='oFHtX'><tfoot id='oFHtX'></tfoot><dl id='oFHtX'><fieldset id='oFHtX'></fieldset></dl></div>
                            <tfoot id='oFHtX'></tfoot>
                            主站蜘蛛池模板: 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 氢氧化钙设备, 氢氧化钙生产线-淄博惠琛工贸有限公司 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 谈股票-今日股票行情走势分析-牛股推荐排行榜| Duoguan 夺冠集团| 制氮设备_PSA制氮机_激光切割制氮机_氮气机生产厂家-苏州西斯气体设备有限公司 | 营养师网,营养师考试时间,报名入口—网站首页 | 阳光模拟试验箱_高低温试验箱_高低温冲击试验箱_快速温变试验箱|东莞市赛思检测设备有限公司 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 潍坊大集网-潍坊信息港-潍坊信息网 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 代办建筑资质升级-建筑资质延期就找上海国信启航 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 膜片万向弹性联轴器-冲压铸造模具「沧州昌运模具」 | YAGEO国巨电容|贴片电阻|电容价格|三星代理商-深圳市巨优电子有限公司 | 造价工程师网,考试时间查询,报名入口信息-网站首页 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 塑料熔指仪-塑料熔融指数仪-熔体流动速率试验机-广东宏拓仪器科技有限公司 | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 北京燃气公司 用户服务中心 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 谷歌关键词优化-外贸网站优化-Google SEO小语种推广-思亿欧外贸快车 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-沼河浸过滤器 | 广州企亚 - 数码直喷、白墨印花、源头厂家、透气无手感方案服务商! | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 成都顶呱呱信息技术有限公司-贷款_个人贷款_银行贷款在线申请 - 成都贷款公司 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 |