Problem while uploading a binary file using cgi module

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

I test the script from

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.


J. Wang

the code is like this:
import cgi

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

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

More information about the Python-list mailing list