By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

CGI File Uploads and Progress Bars

P: n/a
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
Sep 8 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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
Sep 8 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.