1

I am uploading a file through ajax/jquery. The demo can be found here. This function will output the percentage complete:

   //progress bar function
    function OnProgress(event, position, total, percentComplete)
    {
        //Progress bar
        $('#progressbox').show();
        $('#progressbar').width(percentComplete + '%') //update progressbar percent complete
        $('#statustxt').html(percentComplete + '%'); //update status text
        if(percentComplete>50)
            {
                $('#statustxt').css('color','#000'); //change status text to white after 50%
            }
    }

But how do I get the transfer speed?

When I printed all the variables of OnProgress, I had this:

event: [OBJECT XMLHTTPREQUESTPROGRESSEVENT]

position: 25668

total: 2122576

percentComplete: 2

I have tried to output event.speed but I got undefined.

I do not want calculate the transfer speed on the server-side, and use another ajax request polling simultaneously that returns the amount downloaded, it would not be efficient.

3
  • (positionInBytes / durationInMS) == rateInKBPs Commented Mar 9, 2015 at 13:47
  • 25668 is the position in bytes, right? Commented Mar 9, 2015 at 13:48
  • 1
    yes. you can do something in the event to track time, like event.target.st=event.target.st||Date.now(); duration=Date.now()-event.target.st; Commented Mar 9, 2015 at 13:51

1 Answer 1

2

You could estimate it client side...

The easiest way would be to add a global variable in your javascript, for upload start times.

<script language=javascript>
    var starttime = new Date().getTime();
    function setStartTime(){ 
        starttime = new Date().getTime();
    }
</script>

and in the html

<input type="submit" id="submit-btn" value="Upload" style="display: inline-block;" onclick="setStartTime()"/>

then you will need to add some stuff like this:

//progress bar function
function OnProgress(event, position, total, percentComplete)
{
    //Progress bar
    $('#progressbox').show();
    $('#progressbar').width(percentComplete + '%') //update progressbar percent complete
    var timeSpent = new Date().getTime() - starttime ;
    $('#statustxt').html(percentComplete + '% time spent so far:' + String(timeSpent)); //update status text

    if(percentComplete>50)
        {
            $('#statustxt').css('color','#000'); //change status text to white after 50%
        }
}

now you just need to use the position / timespent to calculate the average speed since beginning to now..

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

1 Comment

thanks edited that part to be what was intended: new Date().getTime() - starttime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.