Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 577

Show
Ignore:
Timestamp:
08/29/05 16:05:54
Author:
fumanchu
Message:

Improved file upload tutorial to demonstrate reading uploaded files in chunks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/test_tutorials.py

    r567 r577  
    158158""" 
    159159        self.getPage('/upload', h, "POST", b) 
    160         self.assertBody(''' 
    161         <html><body> 
     160        self.assertBody('''<html> 
     161        <body> 
    162162            myFile length: 5<br /> 
    163163            myFile filename: hello.txt<br /> 
    164164            myFile mime-type: text/plain 
    165         </body></html> 
    166         ''') 
     165        </body> 
     166        </html>''') 
    167167 
    168168if __name__ == "__main__": 
  • trunk/cherrypy/tutorial/tut09_file_upload.py

    r567 r577  
    1818     
    1919    def upload(self, myFile): 
    20         return """ 
    21         <html><body> 
     20        out = """<html> 
     21        <body> 
    2222            myFile length: %s<br /> 
    2323            myFile filename: %s<br /> 
    2424            myFile mime-type: %s 
    25         </body></html> 
    26         """ % (len(myFile.value), 
    27                myFile.filename, 
    28                myFile.type) 
     25        </body> 
     26        </html>""" 
     27         
     28        # Although this just counts the file length, it demonstrates 
     29        # how to read large files in chunks instead of all at once. 
     30        # CherryPy uses Python's cgi module to read the uploaded file 
     31        # into a temporary file; myFile.file.read reads from that. 
     32        size = 0 
     33        while True: 
     34            data = myFile.file.read(8192) 
     35            if not data: 
     36                break 
     37            size += len(data) 
     38         
     39        return out % (size, myFile.filename, myFile.type) 
    2940    upload.exposed = True 
    3041 

Hosted by WebFaction

Log in as guest/cpguest to create tickets