Q:

Google App Engine Example of Uploading and Serving Arbitrary Files

I'd like to use GAE to allow a few users to upload files and later retrieve them. Files will be relatively small (a few hundred KB), so just storing stuff as a blob should work. I haven't been able to find any examples of something like this. There are a few image uploading examples out there but I'd like to be able to store word documents, pdfs, tiffs, etc. Any ideas/pointers/links? Thanks!

A:

The same logic used for image uploads apply for other archive types. To make the file downloadable, you add a Content-Disposition header so the user is prompted to download it. A webapp simple example:
class DownloadHandler(webapp.RequestHandler):
    def get(self, file_id):
        # Files is a model.
        f = Files.get_by_id(file_id)
        if not f:
            return self.error(404)

        # Set headers to prompt for download.
        headers = self.response.headers
        headers['Content-Type'] = f.content_type or 'application/octet-stream'
        headers['Content-Disposition'] = 'attachment; filename="%s"' % f.filename

        # Add the file contents to the response.
        self.response.out.write(f.contents)

(untested code, but you get the idea :)

