3 messages in com.googlegroups.pylons-discussRe: How to return some byte stream fr...
FromSent OnAttachments
jerryji28 Jan 2008 09:21 
Dalius Dobravolskas28 Jan 2008 10:12 
jerryji28 Jan 2008 13:05 
Subject:Re: How to return some byte stream from a controller?
From:Dalius Dobravolskas (dali@public.gmane.org)
Date:01/28/2008 10:12:38 AM
List:com.googlegroups.pylons-discuss

jerryji wrote:

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...)

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