Trouble with cgi.FieldStorage() on iis/asp

DeepBlue DeepBlue at DeepBlue.org
Fri Nov 9 11:07:54 EST 2001


To find the name of the file (if I understand you correctly) check out the
following from the cgi file upload script by Tim Middleton:
Say you have the following form:

<form action="%s" method="POST" enctype="multipart/form-data">
Name of File 1: <input name="file.1" type="file" size="35">
<BR>
Name of File 2: <input name="file.2" type="file" size="35">
<BR>
Name of File 3: <input name="file.3" type="file" size="35">
<BR>
Name of File 4: <input name="file.4" type="file" size="35">
<BR>
Name of File 5: <input name="file.5" type="file" size="35">
<BR>
Name of File 6: <input name="file.6" type="file" size="35">
(rest of form etc.........)

    import string etc......
    data = cgi.FieldStorage()
    f = 1
    while f:
        key = "file.%s" % f
        if data.has_key(key):
            fn = data[key].filename
            if not fn:
                f = f + 1
                continue
            if string.rfind(data[key].filename,"\\") >= 0:
                fn = fn[string.rfind(data[key].filename,"\\"):]
            if string.rfind(data[key].filename,"/") >= 0:
                fn = fn[string.rfind(data[key].filename,"/"):]
            if string.rfind(data[key].filename,":") >= 0:
                fn = fn[string.rfind(data[key].filename,":"):]

            o = open(dirUpload+os.sep+fn,"wb")
            o.write(data[key].value)
            o.close()

            fnList.append(fn)
            kbList.append(len(data[key].value))
            kbCount = kbCount + len(data[key].value)
            f = f + 1
        else:
            f = 0

This works flawlessly with me, and file is found with no problems.
Hope this helps.
DeepBlue

"Max M" <maxm at normik.dk> wrote in message
news:3bebd556$0$25387$edfadb0f at dspool01.news.tele.dk...
> 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