Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:46:35+00:00 2026-05-26T15:46:35+00:00

I am using HTML5 for uploading files. I have a button click event attached

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:46:35+00:00Added an answer on May 26, 2026 at 3:46 pm

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

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

    Example: http://jsfiddle.net/wJt8A/


    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);
    

    Example: http://jsfiddle.net/rC63r/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm uploading multiple files via XmlHTTPRequest and HTML5. I have the uploading working fine,
I have read the following tutorial Uploading Files To the Server Using PHP and
I have a link where I need to be click it automatically using jquery
I am uploading large files to an ASP.NET server using a standard HTML <input>
I have been using to upload an image file from HTML5/JS and using PHP
I'm having a problem uploading files on my production server. I'm using the django-stdimage
When uploading one file using <input type=file /> in Opera, it works as expected.
I'm experimenting with using html5 and css counters to number the figures in a
I am looking at using HTML5 Canvas element for my upcoming project. I want
I'd like to start using HTML5's basic features, but at the same time, keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.