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

        <bdo id='CzGDv'></bdo><ul id='CzGDv'></ul>
      1. <small id='CzGDv'></small><noframes id='CzGDv'>

      2. <legend id='CzGDv'><style id='CzGDv'><dir id='CzGDv'><q id='CzGDv'></q></dir></style></legend>

        中止 xmlhttprequest

        Aborting the xmlhttprequest(中止 xmlhttprequest)
      3. <small id='bteMm'></small><noframes id='bteMm'>

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

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

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

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 HTML5 上传文件.我有一个按钮单击事件附加到函数 uploadFile().它工作正常.我还有一个单独的按钮来取消上传.我知道我们需要调用 xhr.abort() 但如何访问 uploadCanceled 函数中的 xhr 对象?我可以使 xhr 对象成为全局对象,但这不是正确的方法.有人可以在这里指导我吗?

                  I am using HTML5 for uploading files. I have a button click event attached to the function uploadFile(). It works fine. I also have a separate button to cancel the upload. I know we need to call xhr.abort() but how do I access the xhr object in the uploadCanceled function? I can make the xhr object global but that is not the proper way. Can someone guide me here?

                  function uploadFile(){ 
                      var filesToBeUploaded = document.getElementById("fileControl"); 
                      var file = filesToBeUploaded.files[0]; 
                      var xhr= new XMLHttpRequest(); 
                      xhr.upload.addEventListener("progress", uploadProgress, false);
                      xhr.addEventListener("load", uploadComplete, false);
                      xhr.addEventListener("error", uploadFailed, false);
                      xhr.addEventListener("abort", uploadCanceled, false);
                  
                  
                      xhr.open("POST", "upload.php", true); 
                  
                      var fd = new FormData();
                      fd.append("fileToUpload", file);
                       xhr.send(fd); 
                  }
                  
                  
                      function uploadCanceled(evt) {
                          alert("Upload has been cancelled");
                      } 
                  

                  干杯

                  推荐答案

                  addEventListener 将设置 uploadCanceled 的上下文 (this) 为 <代码>xhr:

                  addEventListener will set the context (this) of uploadCanceled to xhr:

                  function uploadCanceled(evt) {
                      console.log("Cancelled: " + this.status);
                  }
                  

                  示例:http://jsfiddle.net/wJt8A/

                  如果您需要通过取消"单击触发 xhr.abort,则可以返回引用并在此之后添加所需的任何侦听器:

                  If, instead, you need to trigger xhr.abort through a "Cancel" click, you can return a reference and add any listeners you need after that:

                  function uploadFile() {
                      /* snip */
                      xhr.send(fd);
                  
                      return xhr;
                  }
                  
                  document.getElementById('submit').addEventListener('click', function () {
                      var xhr = uploadFile(),
                          submit = this,
                          cancel = document.getElementById('cancel');
                  
                      function detach() {
                          // remove listeners after they become irrelevant
                          submit.removeEventListener('click', canceling, false);
                          cancel.removeEventListener('click', canceling, false);
                      }
                  
                      function canceling() {
                          detach();
                          xhr.abort();
                      }
                  
                      // detach handlers if XHR finishes first
                      xhr.addEventListener('load', detach, false);
                  
                      // cancel if "Submit" is clicked again before XHR finishes
                      submit.addEventListener('click', canceling, false);
                  
                      // and, of course, cancel if "Cancel" is clicked
                      cancel.addEventListener('click', canceling, false);
                  }, false);
                  

                  示例:http://jsfiddle.net/rC63r/1/

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
                  quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)

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

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

                    <bdo id='fJZzR'></bdo><ul id='fJZzR'></ul>

                      • <tfoot id='fJZzR'></tfoot>
                          <tbody id='fJZzR'></tbody>

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