The basic technique for adding progress bars is relatively simple. With jQuery:
- Install the Ajax File Upload extension.
- Install the periodic execution extension.
- Register some Ajax event callbacks to reveal the progress bar on the page when the upload starts and also to catch errors.
- Call the $.ajaxFileUpload function with the URL of the upload handler script, the id of the file input element, and the callback function to handle output from the upload handler.
- Have the upload handler return a Json object with an id for the upload.
- Call your progress bar update function with the periodic execution model: $.periodic(updateProgressBar);
- The updateProgressBar function should fetch the download status from a server-side script, supplying the upload id and a callback function: $.getJSON("fetchProgress", {id: id}, function(data) {/* update progress bar with data.percentDownloaded*/});
- The fetchProgress script should return upload progress information in a Json object. I return percentDownloaded, but you can include anything you'd like, such as upload rate. You should also provide error information here, such as if the upload failed.
- The callback function for fetchProgress should update the page to reflect updated progress. For instance, updating a percentage to completion could be as simple as $("#percent").empty().append(data.percentDownloaded);
In the meantime, there are a number of action items that require your attention. First, calculate the file length by taking the HTTP request Content-Length field and subtracting the size of everything which is not the file in order to yield the file length. I did this with the following shoddy algorithm:
- Extract the MIME boundary from the Content-Disposition field.
- Subtract the size of the MIME boundary twice (there is a boundary on both sides of the file).
- Subtract 2 because the second boundary has a trailing "--".
- Subtract 4 because each boundary has a trailing two-character newline (\r\n).
- Subtract 8 because my numbers were always off by exactly 8. I'm not sure where this additional 8 is coming from.
- Improve algorithm so that it's robust enough to work with most browsers
- Submit a patch to python's FieldStorage class to support this algorithm
- Submit a bug report to Mozilla requesting that they supply content length in file uploads