CGI POST binary file upload?

Ilariu Raducan lale at
Tue Dec 5 21:27:41 CET 2000

In class _sockobject in the file change the 'r' mode with 'rb'

  def makefile(self, mode='r', bufsize=-1):
        #MODIFIED   return _fileobject(self._sock, 'rb', bufsize)
        return _fileobject(self._sock, 'rb', bufsize)

I tried the POST method with the same results as you and I got the same
After I changed to 'rb' is working

And remember to open any binary file with 'rb' mode in windows



"Bill Seitz" <fluxent at> wrote in message
news:90j8j0$cmt$1 at
> I've tweaked some code which someone else posted a while back, to allow
> a CGI (behind Apache on Win2K) accept a POST with a file upload, and
> write it out to the file tree.
> It works fine for text/html files (though the byte count is slightly
> off: line endings?). But for binary files it's a total mess. For GIFs I
> generally only get ~100 bytes, for JPEGs ~500bytes. MsWord and PDF
> files also don't work, though I usually get a bigger end result.
> I don't think the problem is the file writing, it's the
> receiving/reading. I say this because I also output in my response page
> len(data['image'].value)
> and it agrees with the size of the output file.
> I tried adding
> msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
> up at the top of my code, but (a) it doesn't seem to matter, and (b)
> I'm not sure whether I'm doing it right.
> thx
> Sent via
> Before you buy.

More information about the Python-list mailing list