CGI File Uploads and Progress Bars

D

Doug Helm

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 = lobjIncomingFile.file.read(afcommon.OneMeg)
if not lstrData:
break
lobjFile.write(lstrData)
llngBytes += long(len(lstrData))
lobjFile.close()

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
following:

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 = lobjIncomingFile.file.read()
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
content_header?

Thanks much for any insight that you might have.

Doug
 
D

Diez B. Roggisch

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
content_header?

http://www.faqs.org/rfcs/rfc1867.html

It seems that _maybe_ you can use the content-length http header. But it
looks as if it's not mandatory in this case - and of course uploading
multiple files will only allow for an overall percentage.


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top