CGI File Uploads and Progress Bars

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug Helm

    #1

    CGI File Uploads and Progress Bars

    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 = lobjIncomingFil e.file.read(afc ommon.OneMeg)
    if not lstrData:
    break
    lobjFile.write( lstrData)
    llngBytes += long(len(lstrDa ta))
    lobjFile.close( )

    Assume that lobjIncomingFil e is actually a file-type element coming from
    CGI.FieldStorag e. 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 = lobjIncomingFil e.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


  • Diez B. Roggisch

    #2
    Re: CGI File Uploads and Progress Bars

    > So, bottom line: Does anyone know how to get the size of the incoming file[color=blue]
    > data without reading the whole thing into a string? Can I do something with
    > content_header?[/color]

    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

    Comment

    Working...