久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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="qwawimqqmiuu" 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 下載文件的進度條

        Show a progress bar for downloading files using XHR2/AJAX(顯示使用 XHR2/AJAX 下載文件的進度條)
          <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="qwawimqqmiuu" 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 下載文件的進度條的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

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

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

                  提前致謝.

                  JS

                  //文件下載filelist.on('click', '.download_link', function(e){e.preventDefault();var id = $(this).data('id');$(this).parent().addClass("download_start");$.ajax({xhr: 函數 () {var xhr = 新窗口.XMLHttpRequest();//處理下載進度xhr.addEventListener("進度", function (evt) {如果(evt.lengthComputable){var percentComplete = evt.loaded/evt.total;console.log(percentComplete);}}, 錯誤的);返回xhr;},完成:函數(){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="刪除這個?"></i></a></div></div></li>

                  解決方案

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

                  另一個需要注意的重要事項 - 為了使 lengthComputable 為真 - 您的服務器必須發送帶有文件大小的 Content-Length 標頭.

                  這里是javascript代碼:

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="a1" data-filename="filename.xml">點擊下載</div><腳本>$('#a1').click(function() {var that = this;var page_url = 'download.php';var req = new XMLHttpRequest();req.open("POST", page_url, true);req.addEventListener("進度", function (evt) {如果(evt.lengthComputable){var percentComplete = evt.loaded/evt.total;console.log(percentComplete);}}, 錯誤的);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 代碼示例:

                  <塊引用>

                  請注意,我添加了一個睡眠來模擬慢速連接以在 localhost 上進行測試.
                  您應該在生產中刪除此 :)

                  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 :)

                  這篇關于顯示使用 XHR2/AJAX 下載文件的進度條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  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標頭) - IT屋-程序員軟件開發技術分
                  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 部分內容)

                    <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="qwawimqqmiuu" id='oFHtX'><tfoot id='oFHtX'></tfoot><dl id='oFHtX'><fieldset id='oFHtX'></fieldset></dl></div>
                            <tfoot id='oFHtX'></tfoot>
                            主站蜘蛛池模板: 日本三级在线 | 国产区在线 | a免费视频 | 欧州一区二区三区 | 亚洲 欧美 精品 | 一区二区三区欧美大片 | 精品久久国产 | 高清成人免费视频 | 91精品中文字幕一区二区三区 | 日韩欧美电影在线 | 色综合久久久 | 久久精品国产免费一区二区三区 | 三级黄片毛片 | 久久乐国产精品 | 在线亚洲一区二区 | 日本精品久久久一区二区三区 | 国产精品久久久久一区二区三区 | 黄免费在线 | 国产乱人伦精品一区二区 | 羞羞视频免费观 | 成人免费视频一区 | 免费久 | 欧美最猛性xxxxx亚洲精品 | 国产成人精品免高潮在线观看 | 91玖玖| 五月天激情电影 | 亚洲va欧美va天堂v国产综合 | av中文天堂 | 羞羞免费网站 | 一二三四在线视频观看社区 | 色天堂影院 | 亚洲人精品 | 麻豆91精品91久久久 | 亚洲一区二区三区四区五区午夜 | 影音先锋中文字幕在线观看 | 亚洲国产精品人人爽夜夜爽 | 欧美精品在线视频 | 久久i| 日韩精品在线网站 | 国产一区欧美一区 | 一本色道精品久久一区二区三区 |