Search code examples
javascriptjquerymathuploadify

Calculating upload speed with Uploadify plugin


For some reason the latest version of Uploadify has done away with reporting the users upload speed from the onProgress event (unless I'm missing something) making it harder for me to report the users upload speed.

Basically, the onProgress event gets fired periodically (although not at set intervals, it seems to vary a bit) and reports the total bytes uploaded so far (bytesUploaded), and the total amount of bytes to upload (bytesTotal).

How can I use these two metrics to calculate the users upload speed in kb/s? I know I'd have to make a calculation based on those two values and also use a Javascript date/time tracker or something but I can't get my head around how I'd do it.

Thanks!


Solution

  • I assume that you are able to get the elapsed time between each firing of the onprogress event. To calculate download speed, you'll also need to keep track of how much was downloaded each time. In other words, save the bytesUploaded each time, then subtract the old value from the new value. Now you know how much time has elapsed, and how much was downloaded during that time span. Simply divide the difference in bytesUploaded by the elapsed time to get the upload speed in bytes/second. Divide by 1024 to get kilobytes/second. I usually check if the number is still greater than 1024 after the second division to know if the speed is greater than one megabyte per second, and if so, divide by 1024 again to get that number.

    In summary: get the time elapsed and the numbers of bytes uploaded in that time and divide the number of bytes uploaded by the time span to get the download speed.