Canceling $ .post before completion

I am using $ .post to upload files to the server. The file will take some time to download based on the file size. So I want to give the user the option to cancel the upload of the file ($ .post) before the upload is complete:

var file = document.getElementById('file').files[0];
$.post('/upload.aspx',
      {file : file},
      function(data){
       //upload completed
      }
   );

      

Any suggestion?

+3


source to share


1 answer


The main XMLHttpRequest

object has a abort

method
in most browsers, so:



var file = document.getElementById('file').files[0];
var xhr = $.post('/upload.aspx',
  {file : file},
  function(data){
   //upload completed
  }
);

// Later if aborting it
if (xhr.abort) {
    xhr.abort();
}

      

+1


source







All Articles