tut09_files.py (1632B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\tutorial\tut09_files.py 2 import os 3 localDir = os.path.dirname(__file__) 4 absDir = os.path.join(os.getcwd(), localDir) 5 import cherrypy 6 from cherrypy.lib import static 7 8 class FileDemo(object): 9 10 def index(self): 11 return '\n <html><body>\n <h2>Upload a file</h2>\n <form action="upload" method="post" enctype="multipart/form-data">\n filename: <input type="file" name="myFile" /><br />\n <input type="submit" />\n </form>\n <h2>Download a file</h2>\n <a href=\'download\'>This one</a>\n </body></html>\n ' 12 13 index.exposed = True 14 15 def upload(self, myFile): 16 out = '<html>\n <body>\n myFile length: %s<br />\n myFile filename: %s<br />\n myFile mime-type: %s\n </body>\n </html>' 17 size = 0 18 while True: 19 data = myFile.file.read(8192) 20 if not data: 21 break 22 size += len(data) 23 24 return out % (size, myFile.filename, myFile.content_type) 25 26 upload.exposed = True 27 28 def download(self): 29 path = os.path.join(absDir, 'pdf_file.pdf') 30 return static.serve_file(path, 'application/x-download', 'attachment', os.path.basename(path)) 31 32 download.exposed = True 33 34 35 import os.path 36 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 37 if __name__ == '__main__': 38 cherrypy.quickstart(FileDemo(), config=tutconf) 39 else: 40 cherrypy.tree.mount(FileDemo(), config=tutconf)