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");
}
Cheers