[Tutor] Tutor digest, Vol 1 #609 - 14 msgs

Michael P. Reilly arcege@shore.net
Fri, 23 Feb 2001 08:20:14 -0500 (EST)


> Hello Arcege!
> 
> should=B4nt the code read:
> 
>   #### if form.has_key('name') ###
> 
> Instead? =

Nope, I meant "file".  In an INPUT tag, the name= attribute defines the
name in the form.  In my last posting, I called the field "file".

<FORM METHOD=GET ACTION="/cgi-bin/form.cgi">
First name: <INPUT name="first" type="text"><BR>
Last name: <INPUT name="last" type="text"><BR>
</FORM>

Give a URL of "/cgi-bin/form.cgi?first=Sharriff&last=Aina".

And for a file upload:
<FORM METHOD=POST ENCTYPE="multipart/form-data"
  ACTION="/cgi-bin/form.cgi">
Filename: <INPUT name="uploadfile" type="file"><BR>
</FORM>

gives:
--192168202199
Content-disposition: form-data; name="uploadfile"; filename="menu.txt"

Spam, spam, eggs & toast
Spam, eggs, toast and spam
Spam, spam, spam and spam
--192168202199--

Here you would get:
>>> form['uploadfile'].filename
'menu'
>>>

The name is "uploadfile".

  -Arcege

> Message text written by INTERNET:tutor@python.org
> >import cgi
> form =3D cgi.FieldStorage()
> if form.has_key('file'):  # from <INPUT name=3D"file"...>
>     fileitem =3D form['file']
>     if fileitem.file:
>         contents =3D []
>         line =3D fileitem.file.readline()
>         while line:
>             bytes =3D len(len)
>             contents.append( line )
>             line =3D fileitem.file.readline()
>             if fileitem.length !=3D -1 and fileitem.length !=3D bytes:
>                 raise ValueError("data read not same as Content-Length")
>         # get the client-given filename, the contents and length
>         file =3D (fileitem.filename, contents, fileitem.length)<


-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------