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

      • <bdo id='t0inP'></bdo><ul id='t0inP'></ul>
    1. <tfoot id='t0inP'></tfoot>
      <legend id='t0inP'><style id='t0inP'><dir id='t0inP'><q id='t0inP'></q></dir></style></legend>
      <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 class="qwawimqqmiuu" id='t0inP'><tfoot id='t0inP'></tfoot><dl id='t0inP'><fieldset id='t0inP'></fieldset></dl></div>

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

    2. 中止 xmlhttprequest

      Aborting the xmlhttprequest(中止 xmlhttprequest)
        <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 class="qwawimqqmiuu" id='xOOKp'><tfoot id='xOOKp'></tfoot><dl id='xOOKp'><fieldset id='xOOKp'></fieldset></dl></div>
          <tbody id='xOOKp'></tbody>

          <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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在使用 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模板網!

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

                相關文檔推薦

                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 部分內容)

                  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 class="qwawimqqmiuu" id='lkMPS'><tfoot id='lkMPS'></tfoot><dl id='lkMPS'><fieldset id='lkMPS'></fieldset></dl></div>

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

                      <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>
                          主站蜘蛛池模板: aaa级片 | 欧美一区二区视频 | 欧美成人激情视频 | 日韩欧美操 | 日本高清视频在线播放 | 欧美色综合| 亚洲最新在线视频 | 久久一二区 | 国产欧美精品 | 毛片站 | 嫩草视频在线 | 91精品一区 | 久久精品视频在线免费观看 | 性高湖久久久久久久久 | 韩三级在线观看 | 成人av一区二区三区 | 成年免费大片黄在线观看一级 | 欧美一区二区三区精品 | 日韩一区二区三区精品 | 一级欧美一级日韩片免费观看 | 国产在线中文 | 久久久久av | 伊人网综合在线 | 99视频在线播放 | 日韩国产一区二区 | 久久丝袜| 国产小视频在线 | 精品视频在线一区 | 国产亚洲日本精品 | 亚洲男人天堂av | 免费黄色特级片 | 日韩不卡一区二区 | 天天干天天操天天看 | 欧美精品久久久久 | 亚洲一区免费 | 欧美极品一区二区 | 我想看国产一级毛片 | 激情综合五月 | 亚洲毛片 | 一区二区三区精品在线 | 天天综合网7799精品 |