[Tutor] HTTP upload- Second try

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


> Hi Guys, I=B4m sorry to be a bore, but could someone tell my why my code
> fails?
> 
> #### code follows ##
> 
> #!C:/Python/python.exe -u
> 
> print "Content-Type: text/html\n\n"
> 
> 
> import cgi
> #import os
> #
> #
> form =3D cgi.FieldStorage()
> if form.has_key('name'):  # from <INPUT name=3D"newimage"...>
>    imageitem =3D form['name']
>    if imageitem.file:
>        contents =3D form['newimage']
>        tempthumb =3D open('.\medintouch\images\testimage.txt', 'w')
>        tempthumb.write(contents)
> print "<br></br>"
> print "<html><head><title>Image uploaded</title></head>"
> print '<body>'
> print "<br></br>"
> print "saving Image file to disk..."
> print "<br></br>"
> print "<br></br>"
> print '</body>'
> print '</html>'
> 
> #### code end ##

Unless your form had an additional tag with <INPUT name="name"
type="file"> then you will never get to the "form['newimage']" part of
the code.

Try:

form = cgi.FieldStorage()
# is there a newimage being loaded?
if form.has_key("newimage") and form["newimage'].file:
  fp = form["newimage"].file
  # write to some file with an "image" extension instead of a text file
  tempthumb = open('.\\medintouch\\images\\testimage.img', 'w')
  block = fp.read(8192)
  while block:
    tempthumb.write(block)
    block = fp.read(8192)
...
print '<img src="/images/testimage.img">'

There have been some recent discussions here about WinXX pathnames and
the backslash character.  Your file would also be written with an
embedded tab character. :(

Good luck :)
  -Arcege

PS: I verified this with Apache on FreeBSD, changing pathnames as
necessary.

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