user1903898
user1903898

Reputation: 75

Ajax request monitoring flow

I have an AJAX Request in a JavaScript script where I GET a file like that:

 $.ajax({
      type: "GET",
      url: "./" + img_type + ".bmp",
      dataType: "html",
  timeout: test_timeout, 
      cache: false, 
      success: function(msg)
      {
      //some stuff
      }
  });

The code itself is correct and works perfectly. Is there a way to know HOW much of the file I've downloaded while the request is still ongoing? I mean, once the request gives to me the success message I know that I've downloaded the entire file, but what if I want to know after two seconds of beginning?

Upvotes: 0

Views: 178

Answers (2)

Licson
Licson

Reputation: 2271

Here's an example:

var xhr = new XMLHttpRequest;
xhr.onprogress = function(e){
    if(e.lengthComputable){
        var progress = e.position || e.loaded;
        var total = e.totalSize || e.total;
        var percent = progress/total*100;
        //do something with progress here
    }
};

xhr.onload = function(){
    var content = xhr.responseText;
    //do something with the result here
};
xhr.open('GET','./'+type+'.bmp',true);
xhr.send();

Upvotes: 0

hunterloftis
hunterloftis

Reputation: 13799

Check out the "monitoring progress" section here:

https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest

Upvotes: 2

Related Questions