Trouble with cgi.FieldStorage() on iis/asp 
Author Message
 Trouble with cgi.FieldStorage() on iis/asp

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 ##########################

<%
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()
%>



Tue, 27 Apr 2004 21:08:38 GMT  
 Trouble with cgi.FieldStorage() on iis/asp
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


Quote:
> 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 ##########################

> <%
> 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()
> %>



Wed, 28 Apr 2004 00:07:54 GMT  
 Trouble with cgi.FieldStorage() on iis/asp

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

Yes, but as far as I can tell, you are using the cgi module in a cgi script.
I am trying to do the same thing under Active Server Pages on Microsoft IIS,
and there I dont have the same enviroment variables available. So calling
FieldStorage() without any argumants will not work there.

Thats why I call FieldStorage with parameters. But apparantly it isn't
working like i believed.

Regards Max M



Wed, 28 Apr 2004 01:11:01 GMT  
 Trouble with cgi.FieldStorage() on iis/asp
maxm
Sorry, I should have been careful.
I did not notice the ASP part :)  although i see now it was all over the
post.  I just saw the cgi part.
DeepBlue


Quote:


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

> Yes, but as far as I can tell, you are using the cgi module in a cgi
script.
> I am trying to do the same thing under Active Server Pages on Microsoft
IIS,
> and there I dont have the same enviroment variables available. So calling
> FieldStorage() without any argumants will not work there.

> Thats why I call FieldStorage with parameters. But apparantly it isn't
> working like i believed.

> Regards Max M



Wed, 28 Apr 2004 01:28:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Solution: using the FieldStorage in .asp pages under IIS

2. Trouble with IIS Python ASP

3. Object Rexx on IIS using CGI and ASP

4. Using cgi.FieldStorage with enctype=multipart/form-data in SimpleHTTPServer = nothing

5. cgi.FieldStorage and POST

6. cgi.FieldStorage() problem

7. cgi.FieldStorage()

8. cgi.FieldStorage not parsing query string

9. cgi FieldStorage __str__ problem - I think...

10. cgi FieldStorage strangeness

11. IIS hangs when using Topspeed ODBC with Active Server Pages (ASP)

 

 
Powered by phpBB® Forum Software