Problem while uploading a binary file using cgi module

J. Wang jw9 at cse.buffalo.edu
Sun Feb 3 21:49:59 EST 2002


I test the script from
http://www.python.org/doc/essays/ppt/sd99east/sld058.htm.

My problem is that it works fine with a text file, but failed everytime
with a binary file such as jpeg file, even a very small (5k) binary file.
I tried read(), readline(), readlines(). It just doesn't work. Every time
the FieldStorage() only read a very small portion of the binary file and
then stop.

what have I done wrong? By the way, i need to use it in a webpage, so
ftplib module is not a alternative option.

Thanks!

J. Wang

the code is like this:
#!e:/Python22/python.exe
import cgi

print 'Content-type: text/html\n'
form = cgi.FieldStorage()
if not form:
	print """
	<form action="test.py" method="POST" enctype="multipart/form-data">
	<input type = "file" name = "filename">
	<input type ="submit">
	</form>
	"""

elif form.has_key("filename"):
	item = form["filename"]
	if item.file:
		while 1:
			data = item.file.readline()
			fp = file('1.jpg','wb')
			if data:
				fp.write(data)
			else:
				break
			fp.close()






More information about the Python-list mailing list