Calculate upload speed
If you want to test the upload speed I don't see why you would want to send this specific image.
I would rather send raw data.
Here is an example :
var http = new XMLHttpRequest();
var startTime, endTime;
var url = "script_that_whill_handle_post.php";
var myData = "d="; // the raw data you will send
for(var i = 0 ; i < 1022 ; i++) //if you want to send 1 kb (2 + 1022 bytes = 1024b = 1kb). change it the way you want
{
myData += "k"; // add one byte of data;
}
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", myData .length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
endTime = (new Date()).getTime();
ShowData();
}
}
startTime = (new Date()).getTime();
http.send(myData);
(Use the same showData function)
You could also send the data of the image instead of the letter "k" multiple times but it would need more code and I won't see any improvements doing this.
Hope it helped
For ajax uploads:
var lastNow = new Date().getTime();
var lastKBytes = 0;
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(e) {
if (e.lengthComputable) {
var now = new Date().getTime();
var bytes = e.loaded;
var total = e.total;
var percent = bytes / total * 100;
var kbytes = bytes / 1024;
var mbytes = kbytes / 1024;
var uploadedkBytes = kbytes - lastKBytes;
var elapsed = (now - lastNow) / 1000;
var kbps = elapsed ? uploadedkBytes / elapsed : 0 ;
lastKBytes = kbytes;
lastNow = now;
console.log(mbytes.toFixed(2) + "MB (" + percent.toFixed(2) + "%) " + kbps.toFixed(2) + "KB/s");
}
}, false);