title: Cherrypy: How get rawdata from uploaded file?
body: <p>i have a problem with upload a file using cherrypy 3.2.4. (Python 2.7)
  I can't get rawdata of uploaded file. I've tried to debug to find how to get data value from response but without success. Have anybody an idea how to solve this ?</p>
  
  <p>Piotr</p>
  
  <p>This is code which I use:</p>
  
  <pre><code>    def index(self):
      return """
      &lt;html&gt;&lt;body&gt;
          &lt;h2&gt;Upload a file&lt;/h2&gt;
          &lt;form action="upload" method="post" enctype="multipart/form-data"&gt;
          filename: &lt;input type="file" name="myFile" /&gt;&lt;br /&gt;
          &lt;input type="submit" /&gt;
          &lt;/form&gt;
          &lt;h2&gt;Download a file&lt;/h2&gt;
          &lt;a href='download'&gt;This one&lt;/a&gt;
      &lt;/body&gt;&lt;/html&gt;
      """
  index.exposed = True
  
  def upload(self, myFile):
      out = """&lt;html&gt;
      &lt;body&gt;
          myFile length: %s&lt;br /&gt;
          myFile filename: %s&lt;br /&gt;
          myFile mime-type: %s
      &lt;/body&gt;
      &lt;/html&gt;"""
  
  
      cherrypy.log('-'*60)
      cherrypy.log('0: ' + str(dir(myFile)))
      cherrypy.log('-'*60)
      cherrypy.log('2: ' + str(dir(cherrypy.request.headers)))
      cherrypy.log('-'*60)
      cherrypy.log('4: ' + str(dir(cherrypy.request.rfile)))
      cherrypy.log('-'*60)
      cherrypy.log('6: ' + str(cherrypy.request.rfile.readline))
      cherrypy.log('-'*60)
      cherrypy.log('8: ' + str(cherrypy.request.rfile.read()))
      cherrypy.log('-'*60)
      cherrypy.log('-'*60)
      cherrypy.log('-'*60)
      # Although this just counts the file length, it demonstrates
      # how to read large files in chunks instead of all at once.
      # CherryPy reads the uploaded file into a temporary file;
      # myFile.file.read reads from that.
      size = 0
      while True:
          data = myFile.file.read(8192)
          if not data:
              break
          size += len(data)
      #cherrypy.log('myFile: %s' % str(dir(myFile)))
      #cherrypy.log('File: %s' % str(myFile.read_into_file))
      #myFile.read_into_file('a.txt')
      return out % (size, myFile.filename, myFile.content_type)
  upload.exposed = True
  </code></pre>
  
  <p>and this is output:</p>
  
  <pre><code>[27/Jul/2014:13:38:32]  0: ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attempt_charsets', 'boundary', 'charset', 'content_type', 'default_content_type', 'default_proc', 'file', 'filename', 'fp', 'from_fp', 'fullvalue', 'headers', 'length', 'make_file', 'maxrambytes', 'name', 'next', 'params', 'part_class', 'parts', 'process', 'processors', 'read', 'read_headers', 'read_into_file', 'read_lines_to_boundary', 'readline', 'readlines', 'type', 'value']
  [27/Jul/2014:13:38:32]  2: ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__','__weakref__', 'clear', 'copy', 'elements', 'encode', 'encode_header_items', 'encodings', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'output', 'pop', 'popitem', 'protocol', 'setdefault', 'update', 'use_rfc_2047', 'values', 'viewitems', 'viewkeys', 'viewvalues']
  [27/Jul/2014:13:38:32]  4: ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'close', 'read', 'readline', 'readlines', 'remaining', 'rfile']
  [27/Jul/2014:13:38:32]  6: &lt;bound method KnownLengthRFile.readline of &lt;cherrypy.wsgiserver.wsgiserver2.KnownLengthRFile object at 0x0000000002FF8D30&gt;&gt;
  [27/Jul/2014:13:38:32]  8:
  [27/Jul/2014:13:38:32] "POST /upload HTTP/1.1" 200 172 "http://localhost:8080/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"
  </code></pre>
  
  <p><strong>Edited:</strong></p>
  
  <p>I tried and found simple solution :</p>
  
  <pre><code>def upload(self, myFile):
      lcHDRS = {}
      for key, val in cherrypy.request.headers.iteritems():
          lcHDRS[key.lower()] = val
  
      incomingBytes = int(lcHDRS['content-length'])
      content = myFile.file.read(incomingBytes)
      file = open ('./upload/' + file.filename,"wb")
      file.write(content)
      file.close()                
  
      return 'File was uploaded...'
  </code></pre>