我正在使用 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模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)