Problems with uploading binary files using cgi module

Shichang Zhao shichang at icubed.com
Sun Mar 5 16:51:22 EST 2000


I am not sure there is a bug in Python cgi module or I did something wrong. 
Here is what I am trying to achieve:

Goal:
     Uploading a binary file, say a Microsoft Word file named test.doc, to 
Web server's cgi-bin directory with a name
     tmp.doc.

The script:
try:
  import cgi
  form = cgi.FieldStorage(keep_blank_values=1, strict_parsing=1)
  fileitem = form["UPL_1"]       #UPL_1: The file name given in HTML form
  if fileitem.file:
     filename = fileitem.filename
     f = open("tmp.doc", "wb")
     f.write(fileitem.value)
     f.close()
  else:
     filename = ""
except:
  pass
print "Content-type: text/html\n\n"
print "Thanks."


After the file is uploaded, I compared the original file's size (19,456 
bytes) with the file saved onto the server's cgi-bin directory (tmp.doc, 
size: 13,073 bytes). Some of the bytes are lost after the file is uploaded.

I searched Python's web site and dejanews web site, no where I can get an 
answer. Please point out what I have done wrong, and how to correct the 
problem.

Any suggestions/hints would be very helpful.

Thanks.






More information about the Python-list mailing list