title: Image file uploaded to server is corrupted/format not supported; python, cgi and html
body: <p>I have code that is to upload an image file from the user's system, but on the server side, the uploaded image is not displayed and when I try to open manually, it says file format not supported
  Here is the html code</p>
  
  <pre><code>  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;DICON Employee Form&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form enctype = "multipart/form-data" method = "post" action = "http://localhost/cgi-bin/DICON_FORM.py"&gt;
  &lt;p&gt;File: &lt;input type = "file" name = "filename" /&gt;&lt;/p&gt;
  &lt;input type = "submit" value = "Upload" /&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the cgi-python script</p>
  
  <pre><code>#!c:\Python27\python.exe
  
  import cgi
  import cgitb
  import os
  import MySQLdb
  
  cgitb.enable()
  form = cgi.FieldStorage()
  
  if not form.has_key("EmployeeID"):
      print "Location: /DICON_FORM.html\n"
  else:
      print """Content-type: text/html
  
      &lt;head&gt;&lt;title&gt;FORM&lt;/title&gt;&lt;/head&gt;
      &lt;body&gt;"""
  
      employeeID = form["EmployeeID"].value
      firstName = form["FirstName"].value
      lastName = form["LastName"].value
      others = form["Others"].value
  
      fileitem = form["filename"]
      fn = ""
  
      if fileitem.filename:
          fn = os.path.basename(fileitem.filename)
          open(fn, 'wb').write(fileitem.file.read()) 
      print "&lt;/body&gt;&lt;/html&gt;"
  </code></pre>