Uploading files

Robert Brewer fumanchu at amor.org
Mon Jan 10 20:36:39 CET 2005

Peter Mott wrote:
> If you upload a file using the cgi module is there any
> way to discover the file name that the user submitted
> as well as the file data? I've googled till I squint
> but I can't find anything.

Working example (for ASP, which uses BinaryRead to get the request

contentLength = int(env['CONTENT_LENGTH'])
content, size = request.BinaryRead(contentLength)

content = StringIO.StringIO(content)
form = cgi.FieldStorage(content, None, "", env, True)

for key in form:
    value = form[key]
        filename = value.filename
    except AttributeError:
        filename = None
    if filename:
        # Store filename, filedata as a tuple.
        self.requestParams[key] = (filename, value.value)
        for subValue in form.getlist(key):
            self.requestParams[key] = subValue

Robert Brewer
Amor Ministries
fumanchu at amor.org

More information about the Python-list mailing list