CGI File Uploads and Progress Bars

Doug Helm dhelm at
Thu Sep 8 05:49:21 CEST 2005

Hey, Folks:

I'm writing a CGI to handle very large file uploads.  I would like to
include a progress bar.  I think I'm about done.  I have code to handle the
file upload, and I think I can add an IFrame to my page which posts to check
file size (so I can tell how many bytes have been received).  My problem is
that I want to provide a % complete.  In order to do that, of course, I need
to know not only the number of bytes received, but also the total number of
incoming bytes.  Here's the heart of the code:

        while afcommon.True:
          lstrData =
          if not lstrData:
          llngBytes += long(len(lstrData))

Assume that lobjIncomingFile is actually a file-type element coming from
CGI.FieldStorage.  It's already been tested to ensure that it is a file-type
element.  Also, assume that I've already opened a file on the server,
referred to by lobjFile (so lobjFile is the target of the incoming data).

If this were a client application opening a file, I would just do the

import os
print os.stat('myfile.dat')[6]

But, of course, this isn't a local file.  In fact, it's not really a file at
all.  It is the contents of a file already rolled up into the HTTP header of
the incoming HTTP request to the Web server.  The CGI module is kind enough
to handle all of the really hard stuff for me (like unpacking and decoding
the header contents, etc.).  But, I still need to know the size of the
incoming file data.

Of course, I could do this by reading the whole thing into a string variable
and then testing the length of the string, as follows:

s =
SizeOfFileIs = len(s)

But that really defeats the purpose, since my whole goal here is to provide
a progress bar, which is contingent upon a "chunking" approach.  Besides,
for the file sizes that I'll be dealing with, I surely wouldn't want to read
the whole thing into memory.

So, bottom line: Does anyone know how to get the size of the incoming file
data without reading the whole thing into a string?  Can I do something with

Thanks much for any insight that you might have.


More information about the Python-list mailing list