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

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

        XHR 中止不会停止文件上传

        时间:2023-05-16

            <legend id='1iR6G'><style id='1iR6G'><dir id='1iR6G'><q id='1iR6G'></q></dir></style></legend>

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

              <tfoot id='1iR6G'></tfoot>

                  <bdo id='1iR6G'></bdo><ul id='1iR6G'></ul>
                    <tbody id='1iR6G'></tbody>

                  <small id='1iR6G'></small><noframes id='1iR6G'>

                • 本文介绍了XHR 中止不会停止文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我已经制作了一个带有进度条的基于 XHR 的文件上传器,我想添加一个功能以在完全上传之前取消上传.简化的代码(在 DOM 准备好后运行):

                  I've made an XHR-based file uploader with progressbar, and I would like to add a feature to cancel the upload before it fully uploaded. The simlified code (runs after DOM ready):

                  var xhr;
                  
                  $('#upload').click(function(){
                      var file = $('#file').get(0).files[0];
                      xhr = new XMLHttpRequest();
                      xhr.upload.onprogress = function(event){
                          if (event.lengthComputable){
                              var percent = event.loaded / event.total;
                              $('#uploadpercent').html(Math.round(percent * 100) + "%");
                          }
                      };
                      xhr.abort = function(){
                          console.log('aborted');
                      };
                      xhr.onload = function(){
                          $('#uploadpercent').html("100%");
                          // ...
                      };
                      xhr.open("POST", "upload.php?filename=" + file.name);
                      xhr.send(file);
                  });
                  
                  $('#cancel').click(function(){
                      xhr.abort();
                  });
                  

                  在调用 xhr.abort() 之后,abort 事件处理程序将 'aborted' 打印到控制台,但它不执行任何其他操作.进度条没有停止,完成后,整个文件被上传.在最新的 Firefox 和 Chrome 上测试.

                  After the xhr.abort() called, the abort event handler prints the 'aborted' to the console, but it doesn't do anything else. The progress bar doesn't stop, and after it finished, the entire file is uploaded. Tested on the latest Firefox and Chrome.

                  我无法弄清楚我的代码有什么问题.

                  I can't figure out what is wrong with my code.

                  我用 jQuery 也试过了,代码:(主要来自 如何异步上传文件?)

                  I've tried the same with jQuery, the code: (mostly from How can I upload files asynchronously?)

                  var jqXHR;
                  
                  $('#upload').click(function(){
                      var file = $('#file').get(0).files[0];
                      jqXHR = $.ajax({
                          type : "POST",
                          url : "upload.php?filename=" + file.name,
                          data : file,
                          xhr: function(){
                              var myXhr = $.ajaxSettings.xhr();
                              if (myXhr.upload){
                                  myXhr.upload.addEventListener('progress', function(event){
                                      if (event.lengthComputable){
                                          var percent = event.loaded / event.total;
                                          $('#uploadpercent').html(Math.round(percent * 100) + "%");
                                      }
                                  }, false);
                              }
                              return myXhr;
                          },
                          success : function(response){
                              $('#uploadpercent').html("100%");
                              if (response.length > 0){
                                  console.log(response);
                              }
                          },
                          error : function(_jqXHR, textStatus){
                              if (textStatus === "abort"){
                                  console.log('aborted');
                              }
                          },
                          cache : false,
                          contentType : false,
                          processData : false
                      });
                  });
                  
                  $('#cancel').click(function(){
                      jqXHR.abort();
                  });
                  

                  令我惊讶的是,这按预期工作.当 jqXHR.abort() 被调用时,'aborted' 文本也被打印出来,进程停止,只有部分文件被上传.问题是我想让我的代码在未来独立于 jquery,但我还不明白为什么我的代码不能以同样的方式工作.

                  To my surprise, this is working as expected. When the jqXHR.abort() called, the 'aborted' text also printed, the process stopped, and only part of the file have been uploaded. The problem is I want to make my code independent of jquery in the future, and I don't understand yet why my code isn't working the same way.

                  推荐答案

                  好的,我注意到事件名称是 onabort.使用 xhr.abort = ... 行,我覆盖了 abort 函数.所以解决方案:

                  Ok, I noticed that the event name is onabort. With the xhr.abort = ... line I overwrote the abort function. So the solution:

                  xhr.onabort = function(){...};

                  xhr.addEventListener("abort", function(){...});

                  这篇关于XHR 中止不会停止文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:传递“("和“)"通过 URI 导致 403 错误,我该如何对其进行编码? 下一篇:在 XDomainRequest 或 ActiveXObject('Microsoft.XMLHTTP'

                  相关文章

                • <tfoot id='U8ATT'></tfoot>
                  <legend id='U8ATT'><style id='U8ATT'><dir id='U8ATT'><q id='U8ATT'></q></dir></style></legend>

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

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