Trouble with cgi.FieldStorage() on iis/asp

Max M maxm at normik.dk
Fri Nov 9 14:08:38 CET 2001


Hi

I am using Pyhon in asp. And I am trying to download a file. I parse my form
content with the methods in the cgi module. This sort-of works. Actually the
code snippet below works fine. It's just that I cannot find the file name of
the opladed file with this example.

So I trie to use:

form = cgi.FieldStorage(fp, pdict)
Instead of:
form = cgi.parse_multipart(fp, pdict)

And then I tried to get at the data with:

form['image'].filename
and
form['image'].file

But apparently my form object is not rigth, as it cannot be indexed. so
form['image'] fails.

I have tried to read the source of the cgi modul, but I don't see why
"FieldStorage()" should have other input parameters than "parse_multipart()"
to work.

Does anybody have a clue?

Regards Max M

######################################

## form.asp ###############################
<form action="formAction.asp" method="post" enctype="multipart/form-data">
    <input type="text" name="title" value="diddi"><br>
    <input type="file" name="image"><br>
    <input type="submit" value="Send">
</form>

## formAction.asp ##########################
<%@ LANGUAGE="Python"%>
<%
import cgi
from StringIO import StringIO

def getQuery():
    # Getting the querystring parsed to 'query' dict
    qs = str(Request.ServerVariables('QUERY_STRING'))
    query = cgi.parse_qs(qs)
    return query

def getForm():
    # Getting the form type & content
    content_type = str(Request.ServerVariables('CONTENT_TYPE'))
    binaryContent = Request.BinaryRead(Request.TotalBytes)

    # Get the header & create the parameterdict to pass to parse_multipart()
    header = cgi.parse_header(content_type)
    pdict = header[1]
    pdict.update({'content-length':binaryContent[1]})

    # Parse and get form as dict
    fp = StringIO(str(binaryContent[0]))
    form = cgi.parse_multipart(fp, pdict)
    fp.close()
    return form

form = getForm()
Save the image
f = open('c:/temp/image.jpg','w+b')
f.write(form['image'][0])
f.close()

query = getQuery()
%>







More information about the Python-list mailing list