| ryah ( @ 2007-03-29 03:18:00 |
| Entry tags: | projects, upload progress bar |
Upload Progress Bars by way of Streaming Javascript
I implemented the XUpload technique of web upload progress bars by streaming javascript (instead of using polling AJAX calls) in Rails. I did this by extending the existing mongrel upload progress plug-in with a new one.
mongrel_streaming_upload_progress-0.1.ge
You'll find an example directory in there with a small Rails application. Start the example with this command
mongrel_rails start -p 3000 -S config/mongrel_upload_progress.confand navigate to http://localhost/files/new in Firefox.The technique is to use
onsubmit to load an iFrame which gets a stream of <script> elements telling the page how to update the progress bar. Questions
- Why is this better?
Because the traditional method is to have the client browser send a question "how much has been uploaded?" every 3 seconds to the web-server only to get the response "12% has been uploaded." Instead the XUpload technique just streams, in one response, "you've uploaded 5%... you've uploaded 8%... you've uploaded 9%." This is less stress on the server and client. - Why not use an AJAX request and eval the response instead of this iFrame hack?
Because (prototype) AJAX requests will not eval the response one statement at a time. (Does anyone have a library that can do that?) - Does it work on Safari?
Not yet, but it should; I can't figure out why. XUpload does. - Why the other iFrame that the form is targeted to?
Because if I don't have that, the second (important) iFrame does not get updated with theonsubmit. (I guess this is because the browser knows it's going to a new page, so won't load new frames.)