How to return some byte stream from a controller?

14 views
Skip to first unread message

jerryji

unread,
Jan 28, 2008, 12:21:41 PM1/28/08
to pylons-discuss
Dear Pyloners,

Greatly appreciated if someone can shed some light on how to return a
byte stream from my controller.

For example, I want /image/123 to return an image so that it could be
referenced in another page as <img src="/image/123" alt="123" />

(similar discussion did appear before --
http://groups.google.com/group/pylons-discuss/msg/f87e2754e6004cb9
but doesn't seem to have an answer...)

Many thanks in advance!

Cheers,
Jerry

Dalius Dobravolskas

unread,
Jan 28, 2008, 1:13:01 PM1/28/08
to pylons-...@googlegroups.com
I use following:

class JpegFileApp(DataApp):
""" Jpeg file serving. """

def __init__(self, filename, content, **kwargs):
self.filename = filename
kwargs['content_type'] = 'image/jpeg'
DataApp.__init__(self, content, None, **kwargs)
self.content_disposition(filename=filename)

def guess_type(self):
return 'image/jpeg'
...
# Somewhere in your controller
def cover(self, id):
...
app = JpegFileApp(id, image_bytes)
return app(request.environ, self.start_response)

Technically you can return image as string and set proper Content-type
in response.headers. E.g:

response.headers['Content-type'] = 'image/jpeg'
return image_bytes

but it was very slow for me. Not sure why.

Regards,
Dalius

jerryji

unread,
Jan 28, 2008, 4:05:33 PM1/28/08
to pylons-discuss
It's been very enlightening! Thank you very much.

Cheers,
Jerry

On Jan 28, 1:13 pm, Dalius Dobravolskas
Reply all
Reply to author
Forward
0 new messages