Upload of binary files

Peter Mott peter.mott at btconnect.com
Thu Mar 13 08:58:28 CET 2003

Agreed - I copied the code, mine originally used read(). But read does not
fix it. Here is an even shorter script:

#! C:/python22/python.exe
import cgi
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

if form.has_key("Data"):
    data = fileinfo.file.read()

print "Content-type: text/plain\n\n"
print len(data)

Text files print their lengths. Graphic files truncate, one to 176 bytes
(actually 176, 177, 176, different each upload). Another gets 3323 bytes up.
A .png file mangages only 4.  The HTML is:


<form action="http://in24/cgi-bin/upload.py"
Text File: <input type="file" name="Data"><br>
<input type="submit" value="-OK-">



"Levente Sandor" <sandorlevi at yahoo.com> wrote in message
news:8b5e42a6.0303121653.6d452403 at posting.google.com...
> "Peter Mott" <peter.mott at btconnect.com> wrote in message
news:<b4nul3$7d2$1 at knossos.btinternet.com>...
> > I am having problems with uploading binary files. I have searched the
> [...]
> >     while 1:
> >         line = fileinfo.file.readline()
> [...]
> It is _absolutely not_ a good idea to use the readline() method on binary
> Use the read() method instead, it should work.
> ----
> levi

More information about the Python-list mailing list