The new ResponseMixin in 1.10 enables you to send non-HTML content from an Albatross page object or module to the browser. This allows you to send back documents such as images, stylesheets, Acrobat files etc.

The example below simply sends the file named in ctx.locals.filename to the browser but the content could be a dynamically generated PDF file.

   1 import mimetypes
   2 
   3 def page_display(ctx):
   4 
   5     # Decide which file to send back
   6     filename = ctx.locals.filename
   7 
   8     # Read the file's content (ignores errors for brevity)
   9     data = file(filename).read()
  10     # Find the MIME type
  11     mime_type = mimetypes.guess_type(filename)[0]
  12     # Set the content-type header
  13 
  14     # Set the MIME type and send the data
  15     ctx.set_header('Content-Type', mime_type)
  16     ctx.send_content(data)

None: Sending_non-HTML_content (last edited 2011-02-15 06:05:17 by localhost)