Web File Uploads Revisited

Posted by Graham Stratton Tue, 24 Oct 2006 23:02:29 GMT

I mentioned in a previous post that showing status for web file uploads is a hard problem. At that point I couldn’t see how it would be possible to watch a file upload from anything other than a simple CGI script, but fortunately James Gardner has kindly proven me wrong. (I don’t really appreciate being proven wrong, but if it’s going to happen then the sooner the better. And in this instance James did give me the results of a day’s coding, for which I am very grateful.)

The Python Web Server Gateway Interface (WSGI) is rapidly becoming the standard gluing point between python web frameworks and various servers. See PEP 333 for the specification and James’ article for more details.

Happily, running an application behind the WSGI doesn’t prevent you from being able to watch the status of an upload. Your application is called as soon as the headers are parsed, so you can do what you like with the body of the request. Letting a slightly modified cgi.FieldStorage deal with it is probably the correct idea. Then you can check the status of a file of known name independently of the upload. Example code from at least one of us will appear at some point to make it all clear!

Asynchronous Uploads

But what I actually want to write about now is asynchronous uploads. Rather than a user having to first select all the files she wants to upload and then wait for them to upload, the upload can be progressing whilst she is selecting later files.

I decided that this is the way to go. Once uploads complete, then the user can be shown thumbnails of the uploaded images, so she can see which ones she has selected and change her mind. Since whichever frame an upload comes from blocks until it gets a response, uploads have to be done in a separate iframe. When the user has selected a file, the iframe in which they selected it is hidden, a new one is created in its place, an upload status is added to the list of selected files, and the upload begins. Simple, huh?

Well, it all went a little wrong. Whilst I had working status for a single upload, Firefox stopped giving me status updates when I tried to upload two files at once. This turned out to be due to the number of persistent connections being limited to two by default (go to about:config and look for network.http.max-persistent-connections-per-server). So that was the end of that for a while. Even changing the config settings (not a useful thing, as you can’t get users to reconfigure their browsers) didn’t really work, as the browser became really unresponsive.

What I’ve now decided to do is to add new files to the end of a queue of files to upload; only one is uploaded at once, but still no potential upload time is wasted. Hopefully I’ll get that new version of the code finished soon, and make it available.

no comments

Comments

(leave url/email »)

   Comment Markup Help Preview comment