0

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?

2
  • You'll need to use the original XMLHttpRequest to do it and only some browsers supports it (Chrome, Firefox, Safari, IE10) Commented Dec 29, 2012 at 16:11
  • this is not a problem, could you post an example please? Commented Dec 29, 2012 at 16:14

2 Answers 2

2

Check out the "monitoring progress" section here:

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.