• <tfoot id='01EB2'></tfoot>
    • <bdo id='01EB2'></bdo><ul id='01EB2'></ul>
    <legend id='01EB2'><style id='01EB2'><dir id='01EB2'><q id='01EB2'></q></dir></style></legend>

      <small id='01EB2'></small><noframes id='01EB2'>

      <i id='01EB2'><tr id='01EB2'><dt id='01EB2'><q id='01EB2'><span id='01EB2'><b id='01EB2'><form id='01EB2'><ins id='01EB2'></ins><ul id='01EB2'></ul><sub id='01EB2'></sub></form><legend id='01EB2'></legend><bdo id='01EB2'><pre id='01EB2'><center id='01EB2'></center></pre></bdo></b><th id='01EB2'></th></span></q></dt></tr></i><div id='01EB2'><tfoot id='01EB2'></tfoot><dl id='01EB2'><fieldset id='01EB2'></fieldset></dl></div>

        使用 XMLHttpRequest 上传大文件时的进度条

        时间:2023-05-15

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

              <tbody id='XHoxM'></tbody>

          1. <tfoot id='XHoxM'></tfoot>
              <legend id='XHoxM'><style id='XHoxM'><dir id='XHoxM'><q id='XHoxM'></q></dir></style></legend>

            1. <small id='XHoxM'></small><noframes id='XHoxM'>

                • <bdo id='XHoxM'></bdo><ul id='XHoxM'></ul>
                • 本文介绍了使用 XMLHttpRequest 上传大文件时的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用 XMLHttpRequest 和 file.slice 将一些大文件上传到服务器.
                  我已经在文档和其他各种链接的帮助下做到了这一点.
                  由于上传大文件是一项漫长的工作,我想为用户提供一个进度条.
                  经过更多阅读后,我遇到了 示例,理论上,正是我需要的.
                  通过获取示例代码并使其适应我的需求,我达到了

                  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]);
                  

                  我的问题是进度事件没有触发.
                  我已经为进度事件尝试了 xhr.addEventListener 和 xhr.upload.addEventListener (一次和一次),但它永远不会触发.onreadystatechange 和 load 事件触发就好了.

                  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

                  更新
                  经过多次尝试,我设法模拟了一个进度,但我遇到了另一个问题:Chrome 的 UI 在上传期间没有更新.
                  现在的代码是这样的

                  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
                  我已经设法修复它并模拟了一个也适用于 chrome 的进度条.
                  我已经用有效的代码示例更新了之前的代码示例.
                  您可以通过减小一次上传的块的大小来更频繁地刷新"栏谢谢你的帮助


                  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 + '%';
                     }
                  }
                  

                  这篇关于使用 XMLHttpRequest 上传大文件时的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何向 roku 中的某些服务器发出 api 请求 下一篇:为什么叫 XMLHttpRequest?

                  相关文章

                    <tfoot id='7QYHU'></tfoot>
                  1. <legend id='7QYHU'><style id='7QYHU'><dir id='7QYHU'><q id='7QYHU'></q></dir></style></legend>
                      <bdo id='7QYHU'></bdo><ul id='7QYHU'></ul>

                    <small id='7QYHU'></small><noframes id='7QYHU'>

                  2. <i id='7QYHU'><tr id='7QYHU'><dt id='7QYHU'><q id='7QYHU'><span id='7QYHU'><b id='7QYHU'><form id='7QYHU'><ins id='7QYHU'></ins><ul id='7QYHU'></ul><sub id='7QYHU'></sub></form><legend id='7QYHU'></legend><bdo id='7QYHU'><pre id='7QYHU'><center id='7QYHU'></center></pre></bdo></b><th id='7QYHU'></th></span></q></dt></tr></i><div id='7QYHU'><tfoot id='7QYHU'></tfoot><dl id='7QYHU'><fieldset id='7QYHU'></fieldset></dl></div>