summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2015-02-02 08:53:27 -0500
committerRyan Petrello <lists@ryanpetrello.com>2015-02-04 13:56:22 -0500
commit3591c9ed0700fa1d703fefc19593e3b59c51c9e9 (patch)
treeb02c0c0ea8dc3ff329c6f86684df69e9ec7ddef5
parent67abf118f817b96972a57e0c3d9cae5a23e68c1c (diff)
downloadpecan-3591c9ed0700fa1d703fefc19593e3b59c51c9e9.tar.gz
Document how to serve static files (with an iterable, not in-memory).
Fixes bug 1221342 Change-Id: Icfad6a262ed211a73291eaead568248f67cfbfc9
-rw-r--r--docs/source/routing.rst54
1 files changed, 54 insertions, 0 deletions
diff --git a/docs/source/routing.rst b/docs/source/routing.rst
index 80deca5..a2bf378 100644
--- a/docs/source/routing.rst
+++ b/docs/source/routing.rst
@@ -427,6 +427,60 @@ The same effect can be achieved with HTTP ``POST`` body variables:
$ curl -X POST "http://localhost:8080/" -H "Content-Type: application/x-www-form-urlencoded" -d "arg=foo"
foo
+Static File Serving
+-------------------
+
+Because Pecan gives you direct access to the underlying
+:class:`~webob.request.Request`, serving a static file download is as simple as
+setting the WSGI ``app_iter`` and specifying the content type::
+
+ import os
+ from random import choice
+
+ from webob.static import FileIter
+
+ from pecan import expose, response
+
+
+ class RootController(object):
+
+ @expose(content_type='image/gif')
+ def gifs(self):
+ filepath = choice((
+ "/path/to/funny/gifs/catdance.gif",
+ "/path/to/funny/gifs/babydance.gif",
+ "/path/to/funny/gifs/putindance.gif"
+ ))
+ f = open(filepath, 'rb')
+ response.app_iter = FileIter(f)
+ response.headers[
+ 'Content-Disposition'
+ ] = 'attachment; filename="%s"' % os.path.basename(f.name)
+
+If you don't know the content type ahead of time (for example, if you're
+retrieving files and their content types from a data store), you can specify
+it via ``response.headers`` rather than in the :func:`~pecan.decorators.expose`
+decorator::
+
+ import os
+ from mimetypes import guess_type
+
+ from webob.static import FileIter
+
+ from pecan import expose, response
+
+
+ class RootController(object):
+
+ @expose()
+ def download(self):
+ f = open('/path/to/some/file', 'rb')
+ response.app_iter = FileIter(f)
+ response.headers['Content-Type'] = guess_type(f.name)
+ response.headers[
+ 'Content-Disposition'
+ ] = 'attachment; filename="%s"' % os.path.basename(f.name)
+
Handling File Uploads
---------------------