Upload of binary files
pmott at comp.leeds.ac.uk
Fri Mar 14 19:22:49 CET 2003
The problem described by myself has been solved in Ben Hutchings post: The
code belows works as we would wish - the binary file is uploaded and the
number of bytes returned to the client:
import msvcrt, os
msvcrt.setmode(0, os.O_BINARY) # stdin = 0
msvcrt.setmode(1, os.O_BINARY) # stdout = 1
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
chunk = fileinfo.file.read()
print "Content-type: text/plain\n\n"
print "Bytes read:",len(chunk)
The point is that without the import of msvcrt (providing low level Windows
support) the cgi module tries to treat all files as text and so the uploads
do not work. I am not a seasoned Python person but I don't think the above
code is very 'Pythonic' but for me it is a perl none the less. Thanks Ben!
"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
> archives and found my problem several times but no answers. The problem is
> that this little form:
> <form action="http://in24/cgi-bin/upload.py" method="post"
> Title: <input type="text" name="Title"><br>
> File: <input type="file" name="Image"><br>
> <input type="submit" value="-OK-">
> with my very standard Python script at the other end will only work for
> ascii files. With a binary file only a fragment is uploaded.
> The Python is:
> if form.has_key("Image"):
> tmp = open("C:/temp/temporary.dat","wb")
> while 1:
> line = fileinfo.file.readline()
> if not line: break
> lines += 1
> which I have copied from various sources (and tried several variants). One
> surprising thing is that a .png file uploads %PNG and no more. This is
> Python 2.2.1c2 on Windows 2000 with Apache 1.3.24 for Windows. I'm using
> localhost under various names.
> Any comments appreciated
More information about the Python-list