0

I have a big data file of size 8 MB. Now while my page is downloading it from the server, I would like to present a status update on how much is downloaded, e.g. 56% is downloaded. The data file in itself is a .js file!

I would like to do it from my javascript. I was thinking of the below option.

1) First measure network speed.
2) Then do a linear interpolation as I know the size of the file.

But measuring netspeed in JS is not very reliable given connection timing, caching, rendering etc etc.

Is there any direct approach, where I can straightway get the size of the partially downloaded file and calculate how much is downloaded so far.

Thanks for your help!

user3001408
  • 310
  • 5
  • 20

1 Answers1

0

You can use xmlhttprequest progress event to find out file upload progress in a HTML5 compatible browser.

var oReq = new XMLHttpRequest();

oReq.upload.addEventListener("progress", updateProgress, false);
// progress on transfers from the server to the client (downloads)
function updateProgress (oEvent) {
  if (oEvent.lengthComputable) {
    var percentComplete = oEvent.loaded / oEvent.total;
    // ...
  } else {
    // Unable to compute progress information since the total size is unknown
 }
}

https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42