upload file via form

Alan Kennedy alanmk at hotmail.com
Tue Dec 4 06:33:42 EST 2001


Hi Iddo,

The solution to this problem should really be in the documentation for
cgi.py

You need to add an "enctype" attribute to your form declaration, so
that the form data gets posted in the body of the HTTP request, rather
than the header.

So your FORM element should read like this

<form action="/cgi-pc/upload.py" method="post"
enctype="multipart/form-data">
 ...
</form>

More details on this topic from the HTML spec at

http://www.w3.org/TR/html4/interact/forms.html#edef-FORM

Regards,

Alan.


Iddo Friedberg <idoerg at cc.huji.ac.il> wrote in message news:<Pine.GSO.4.40_heb2.09.0112040921370.5804-100000 at new-shum>...
> Hi,
> 
> This seems like a FAQ, but looking at the newsgroups, manuals etc. I could
> not find a reference to this.
> 
> I am trying to load a file to the server via a form. Here's the form:
> 
> <html>
> <head>
> <title> Try This</title>
> </head>
> <body>
> <FORM ACTION="/cgi-pc/upload.py" method=POST>
> <p>Upload a file!</p>
> <INPUT TYPE="file" name="userfile" SIZE="30">
> <INPUT TYPE=SUBMIT value=SEND!>
> </FORM>
> 
> And here's the CGI script which is supposed to read and display the
> contents:
> 
> #!/usr/local/bin/python
> import cgi
> 
> form = cgi.FieldStorage()
> if form['userfile'].file:
> 	contents = form['userfile'].file.value
> else:
> 	contents = "Nothing here!"
> 
> print "Content-type: text/html"
> print
> print "<pre>"
> print contents
> print "</pre>"
> 
> I always get a "Nothing here!" What am I doing wrong?
> 
> I'm using an Apache1.3, on Linux RH7.1.
> 
> 
> Yours,
> 
> Iddo



More information about the Python-list mailing list