On Sun, 2012-12-30 at 18:13 -0800, Shu Lin wrote: > Hi, > > > If I have other objects other than String, which is holding a jpg or > png file, I like to return it as Response body, how can I do? > > > I tried a piece of code like this: > > > mimetype = image/png > body = fs.open(access_path, 'rb') > return Response(body, content_type=mimetype) > > > Here "body" is an buffer object from a kind of filesystem which > represents the content in the file. Since, it doesn't support len() > like String. I can't passed the Response check for len(). I tried > app_iter also like this: > > return Response(app_iter=[body], content_type=mimetype) > > But, if I did like this, the wsgiref will complain about the body is > not String type. > > =========== > > 2012-12-29 23:33:59,521 INFO [fs.tahoelafs][MainThread] (4363489744) > Opening file /family/Ada/IMG_0355.JPG in mode rb > > Traceback (most recent call last): > > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", > line 86, in run > > self.finish_response() > > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", > line 127, in finish_response > > self.write(data) > > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", > line 202, in write > > assert type(data) is StringType,"write() argument must be string" > > AssertionError: write() argument must be string > > ========== > > Is there anyway to work around this? > > Thanks a lot in advance! > > -Shu > <http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/static_assets/files.html#serving-file-content-dynamically>
Instead of just using an open file as the app_iter, you can use a pyramid.response.FileIter object. This will chunk the file out in fixed-sized blocks: http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/response.html#pyramid.response.FileIter Or you can just shortcut the whole business and return a FileResponse instead of a response: http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/response.html#pyramid.response.FileResponse - C -- You received this message because you are subscribed to the Google Groups "pylons-devel" group. To post to this group, send email to pylons-devel@googlegroups.com. To unsubscribe from this group, send email to pylons-devel+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.