Commits

Robert Brewer committed a5a6de1

Added download method (serveFile demo) to tutorial 9.

  • Participants
  • Parent commits db00faa

Comments (0)

Files changed (4)

File cherrypy/test/test_tutorials.py

                          'Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/>'
                          '</body></html>')
     
-    def test09FileUpload(self):
-        self.load_tut_module("tut09_file_upload")
+    def test09Files(self):
+        self.load_tut_module("tut09_files")
         
+        # Test upload
         h = [("Content-type", "multipart/form-data; boundary=x"),
              ("Content-Length", "110")]
         b = """--x
             myFile mime-type: text/plain
         </body>
         </html>''')
+    
+        # Test download
+        self.getPage('/download')
+        self.assertStatus("200 OK")
+        self.assertHeader("Content-Type", "application/x-download")
+        self.assertHeader("Content-Disposition", "attachment; filename=ReturnVsYield.pdf")
+        self.assertEqual(len(self.body), 326111)
 
 if __name__ == "__main__":
     helper.testmain()

File cherrypy/tutorial/ReturnVsYield.pdf

Binary file added.

File cherrypy/tutorial/tut09_file_upload.py

-"""Tutorial: File upload"""
-
-import cherrypy
-
-
-class FileUploadDemo(object):
-    
-    def index(self):
-        return """
-        <html><body>
-            <form action="upload" method="post" enctype="multipart/form-data">
-            filename: <input type="file" name="myFile" /><br />
-            <input type="submit" />
-            </form>
-        </body></html>
-        """
-    index.exposed = True
-    
-    def upload(self, myFile):
-        out = """<html>
-        <body>
-            myFile length: %s<br />
-            myFile filename: %s<br />
-            myFile mime-type: %s
-        </body>
-        </html>"""
-        
-        # Although this just counts the file length, it demonstrates
-        # how to read large files in chunks instead of all at once.
-        # CherryPy uses Python's cgi module to read 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)
-        
-        return out % (size, myFile.filename, myFile.type)
-    upload.exposed = True
-
-
-cherrypy.root = FileUploadDemo()
-
-if __name__ == '__main__':
-    # Use the configuration file tutorial.conf.
-    cherrypy.config.update(file = 'tutorial.conf')
-    # Start the CherryPy server.
-    cherrypy.server.start()

File cherrypy/tutorial/tut09_files.py

+"""Tutorial: File upload"""
+
+import os
+localDir = os.path.dirname(__file__)
+absDir = os.path.join(os.getcwd(), localDir)
+
+import cherrypy
+from cherrypy.lib import cptools
+
+
+class FileDemo(object):
+    
+    def index(self):
+        return """
+        <html><body>
+            <form action="upload" method="post" enctype="multipart/form-data">
+            filename: <input type="file" name="myFile" /><br />
+            <input type="submit" />
+            </form>
+        </body></html>
+        """
+    index.exposed = True
+    
+    def upload(self, myFile):
+        out = """<html>
+        <body>
+            myFile length: %s<br />
+            myFile filename: %s<br />
+            myFile mime-type: %s
+        </body>
+        </html>"""
+        
+        # Although this just counts the file length, it demonstrates
+        # how to read large files in chunks instead of all at once.
+        # CherryPy uses Python's cgi module to read 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)
+        
+        return out % (size, myFile.filename, myFile.type)
+    upload.exposed = True
+    
+    def download(self):
+        path = os.path.join(absDir, "ReturnVsYield.pdf")
+        return cptools.serveFile(path, "application/x-download",
+                                 "attachment", os.path.basename(path))
+    download.exposed = True
+
+
+cherrypy.root = FileDemo()
+
+if __name__ == '__main__':
+    # Use the configuration file tutorial.conf.
+    cherrypy.config.update(file = 'tutorial.conf')
+    # Start the CherryPy server.
+    cherrypy.server.start()