File Uploads and Downloads

Web2py supports file uploads and downloads out of the box.

That’s an important part of what I need to do with my site, so I decided to test it early. I created a model with a field whose type was “upload”, like this:


I then used SQLFORM to create a form for this model. The form used a Browse button to allow the user to select a file, and uploaded the file automatically to the server. Web2py put the name of the file into the database field named “proposal”.

The second part of this was downloading the file. Turns out the default controller has a download method that is intended for downloading uploaded files. I was able to use that without modification, by simply providing an appropriate URL (e.g. /default/download/myfilename.pdf).

The file downloaded fine.

I’ll need to put some authentication on this, suitable to my web app’s needs, but the basic functionality works great and out of the box.

I do still need to test to see how this operates on Google App Engine, which doesn’t support writing to the file system. I imagine the file will end up in the datastore as a blob field, but I’ll have to experiment to see.

This entry was posted in Web2py. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *