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!

4

1 Answer 1

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

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

3 Comments

op asked for download, not upload ;)
I am downloading the file, not uploading
Oops :-), but according to link you can use this event for download as well. But I am not sure for eg: how a browser can know file size in advance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.