title: Getting the upload file content to wsgi
body: <p>I'm writing a simple wsgi script for upload a file. Using html form.</p>
  
  <p>Following is the wsgi script,</p>
  
  <pre><code>import os
  import cgi
  import cgitb; cgitb.enable()
  
  
  class upfile(object):
  
      def __init__(self):
          self.script_dir = os.path.dirname(__file__)
          self.errors = []
  
  
      def __call__(self, environ, start_response):
  
  
          f = open(os.path.join(self.script_dir, 'upload.html'))
          self.output = f.read()
          f.close()
  
          self.response_content_type = 'text/html;charset=UTF-8'
          fields = None
          if 'POST' == environ['REQUEST_METHOD'] :        
              fields = cgi.FieldStorage(fp=environ['wsgi.input'],environ=environ, keep_blank_values=1)
              fileitem = fields['file']
              fn = os.path.basename(fileitem.filename) 
              open('uploads/' + fn, 'wb').write(fileitem.file.read())
  
  
          self.output = self.output % {"filepath":str(fields)} # Just to see the contents
  
          response_headers = [('Content-type', self.response_content_type),('Content-Length', str(len(self.output)))]
          status = '200 OK'
          start_response(status, response_headers)
          return [self.output]
  
  application = upfile()
  </code></pre>
  
  <p>Then in my HTML form I put a file field as usual,</p>
  
  <pre><code>&lt;input type='file' name='file' /&gt;
  </code></pre>
  
  <p>My problem is fields (cgi.FieldStorage) is having only the file name. No file contents. I want the file content to uploading not only the file name. </p>
  
  <p>Following is the value coming to the fields variable,</p>
  
  <p><code>"FieldStorage(None, None, [MiniFieldStorage('file', 'CatchSkull-large.jpg'), MiniFieldStorage('email', 'chamith@gmail.com'), MiniFieldStorage('operation', 'Upload'), MiniFieldStorage('current', 'upload')])"</code></p>
  
  <p>Please ignore other fields they are other fields on the form.</p>
  
  <p>Thanks in advance.</p>