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
stream):

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

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

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


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list