Hey guys, i need to track progress of an async ajax request for some file downloads. Is this possible and in what browsers?
+2
A:
This is how I do it most of the time
var req = new XMLHttpRequest();
req.addEventListener("progress", onUpdateProgress, false);
req.addEventListener("load", onTransferComplete, false);
req.addEventListener("error", onTransferFailed, false);
req.addEventListener("abort", onTransferAborted, false);
req.open("GET", ..., true);
...
req.send()
function onUpdateProgress(e) {
if (e.lengthComputable) {
var percent_complete = e.loaded/e.total;
document.getElementById("progressMessage").value = Math.round(percentComplete*100) +"% [ " + Math.round(e.loaded / 1000) + " KB ]";
...
} else {
// Length not known, to avoid division by zero
}
}
function onTransferComplete(e) { ... }
function onTransferFailed(e) { ... }
function onTransferAborted(e) { ... }
And as far as "crossbrowsering" is concerned IE doesn't implement progress support while Firefox and Chrome do.
Vuk
2010-02-11 03:57:14
Great!! Works like a charm. Thank you.
2010-02-11 04:01:11