Special Problem and Solution regarding: CGI Binary File Uploading on Windows

Shichang Zhao shichang at icubed.com
Mon Mar 6 18:45:52 CET 2000

After days of days frustrating, and under x at x.x (seems not a valid email 
address) hints, I finally got a solution regarding binary file uploading 
using Python cgi module on Windows. I think this is something that should 
appear in Python FAQ, or equivalent, since it is so frustrating (at least 
to me).

Problem Description:
    After one uploades a binary file, for example, Microsoft Word or .gif 
file, onto a Web server, one may find that the uploaded file has a file 
size that is less than the original file size (i.e., file is truncated). 
This happens on Windows OS. The following script should fix the problem:


import os, sys
import msvcrt
msvcrt.setmode(0, os.O_BINARY)
import cgi

sys.stderr = sys.stdout

print "Content-type: text/html\n\n"

def uploadFile():
   form = cgi.FieldStorage()
   fileitem = form["UPL_1"]
   f = open("tmp.doc", "wb")

  print "<pre>Thanks for using this service. </pre>"
  import traceback, StringIO
  ferr = StringIO.StringIO()
  traceback.print_exc(file = ferr)
  data = ferr.getvalue()
  print "<pre>%s</pre>" % data

It is my wish that other people will experience less frustration than I 
had, and I shall than x at x.a very much for hints, as shown below.

-----Original Message-----
From:	x at x.x [SMTP:x at x.x]
Sent:	Monday, March 06, 2000 9:50 PM
To:	python-list at python.org
Subject:	Re: Problems with uploading binary files using cgi module

In article <01BF86EC.F2D432E0.shichang at icubed.com>,
Shichang Zhao <shichang at icubed.com> wrote:
> I am not sure there is a bug in Python cgi module or I did somethin 
> Here is what I am trying to achieve:
> Goal:
>      Uploading a binary file, say a Microsoft Word file name test.doc, to 
> Web server's cgi-bin directory with a name
>      tmp.doc.
> After the file is uploaded, I compared the original file's size (19,456
> bytes) with the file saved onto the server's cgi-bin directory (tmp.doc,
> size: 13,073 bytes). Some of the bytes are lost after the file is

Are you by any chance running this off a Windows server? I'm thinking
because you mentioned Microsoft Word; if so, I think I know what the
problem is: Standard input is opened by default in "text" mode, and
you're probably running into an end-of-file character (ascii 26) and
that's causing the file to truncate.

If that's the case, you're going to need to dig into the msvcrt module
and reset sys.stdin at the beginning of your cgi script. If I recall,
you need the "OSF handle" functions. I had to do that once, I can
dig up the code if you need it.

I do remember that doing an os.fdopen(0, "rb") wouldn't do it in
Win32. It was necessary to get into the msvcrt.dll gunk.

More information about the Python-list mailing list