Permalink
Browse files

Adding fileupload recipe

  • Loading branch information...
1 parent b172260 commit 05262462b066d2373dcb9a1ef0a27628ab58f61b @irrelative irrelative committed with anandology Feb 20, 2008
Showing with 45 additions and 0 deletions.
  1. +45 −0 cookbook/fileupload.md
View
@@ -0,0 +1,45 @@
+---
+layout: default
+title: File Upload Recipe
+---
+
+# File Upload Recipe
+
+### Problem
+
+File uploads can be a little tricky if you're not familiar with form uploads, or CGI in general.
+
+### Solution
+
+ import web
+
+ urls = ('/upload', 'Upload')
+
+ class Upload:
+ def GET(self):
+ return """<html><head></head><body>
+ <form method="POST" enctype="multipart/form-data" action="">
+ <input type="file" name="myfile" />
+ <br/>
+ <input type="submit" />
+ </form>
+ </body></html>"""
+
+ def POST(self):
+ x = web.input(myfile={})
+ web.debug(x['myfile'].value) # This is the file contents
+ web.debug(x['myfile'].filename) # This is the filename
+ web.redirect('/upload')
+
+
+ app = web.application(urls, globals())
+
+ if __name__ == "__main__":
+ app.run()
+
+### Hang ups
+
+A couple of things to watch out for:
+
+* The form needs an attribute enctype="multipart/form-data", or this won't work correctly.
+* In the webpy code, a default value is needed (the myfile={} part) if you want it to be imported as a CGI FieldStorage object. If you don't specify the default value, the file will be passed as a string -- this works, but you lose the filename attribute.

0 comments on commit 0526246

Please sign in to comment.