[Tutor] HTTP upload- Second try

D-Man dsh8290@rit.edu
Fri, 23 Feb 2001 10:15:14 -0500


On Fri, Feb 23, 2001 at 04:51:23AM -0500, Sharriff Aina wrote:
| Hi Guys, I=B4m sorry to be a bore, but could someone tell my why my cod=
e
| fails?
|=20
| #### code follows ##
|=20
| #!C:/Python/python.exe -u
|=20
| print "Content-Type: text/html\n\n"
|=20
|=20
| 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)
         tempthumb.close()
| 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>'
|=20
| #### code end ##
|=20
| The test file is nor written at all :-(
|=20

It is always a good idea to close a file when you are done, but I
think the interpreter will do that for you when it terminates (or
collects the file object).  You will also want to change the mode to
"wb" so that you don't get EOL trickery corrupting the file.  Plain
"w" will work on a Unix system because it doesn't play such tricks,
but on Windows it will put a \r (0xD) in front of each \n (0xA).  This
was an ugly bug I found in mutt when I compiled it under cygwin.

Can you write to any file?  Do you have permission?  Is the disk full?
(When debugging, always ask dumb questions, you might be surprised by
the answer ;-))

I don't know if it matters, but you might want to put the "Saving
file ..." before the actual write so the client can (maybe, depends on
network, etc) see a result.  It would also be a good idea to put a
try-except block around the work part of the code so any errors can be
reported to the client.


Umm, I just looked again and noticed you used un-escaped backslashes in
the string.  The file was probably written, but isn't named what you
think it is.  Use forward slashes instead, or use a raw string for the
file name.  The file's path is probably

.\medintouch\images	estimage.txt

(\m and \i don't mean anything do they?)


HTH,
-D