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

    <tfoot id='xOOKp'></tfoot>
    1. <small id='xOOKp'></small><noframes id='xOOKp'>

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

        中止 xmlhttprequest

        时间:2023-10-14

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

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

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

                  问题描述

                  我正在使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何获取“数据"来自 xhr.responseText 的字段? 下一篇:跨域 JSON 请求?

                  相关文章

                  最新文章

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

                    2. <tfoot id='lkMPS'></tfoot>

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

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