Serve images from action call or web service call

90 views
Skip to first unread message

Diego Carvallo

unread,
Jul 16, 2013, 3:51:22 PM7/16/13
to
I am using Web2Py and need to serve images from folder external to Web2py, user images are dynamical and have to be loaded from a different system in the same server therefore they cannot be moved to be contained in the web2py application directory. So I cannot have a relative path to the images. Symbolink links are not an option.

I was thinking the solution could be to serve JPG images from an action call or web service, this way the application can access the local file and return it programatically without having to move a single image. For example a view has the following code:

<li class="file ext_jpg">
 
<figure class="image_container">
 
<img src="controller_name/action_serving_images/unique_id_genereted_for_this_image.jpg" alt="">
 
</figure>
</li>

having the Action:
def action_serving_images(image_id)
   
#obtain image based on it's unique generated id
   
return image

Or for the web service case:
<li class="file ext_jpg">
   
<figure class="image_container">
       
<img src="controller_name/service_serving_images/jpg/image/unique_id_genereted_for_this_image.jpg" alt="">
   
</figure>
</li>

having the Service:
def service_serving_images():
 
return service()

@service.jpg
def image(image_id):
   
#obtain image based on it's unique generated id
   
return image

  1. Is any of these options possible?
  2. How can I fetch the image and return it as a byte stream with the proper content-type so the browser can render it properly?
  3. Do i need to create a special decorator for JPG in the case of the Service? How?

I posted the same question on stackoverflow but no answer yet... I appreciate any help! 

Massimo Di Pierro

unread,
Jul 17, 2013, 5:14:45 AM7/17/13
to web...@googlegroups.com
This is easier than that.

First you create an action like this:

    def serve_image():
        id = request.args(0)
        filename = get_image_filename_from(id)
        stream = open(filename,'rb')
        return response.stream(stream, attachment=True, filename=filename)

Then in your views you do:

    <img src="{{=URL('serve_image',args='1234')}}" />

where 1234 is the id of the image you want.

Diego Carvallo

unread,
Jul 18, 2013, 10:58:59 PM7/18/13
to web...@googlegroups.com

Diego Carvallo

unread,
Jul 18, 2013, 11:16:41 PM7/18/13
to web...@googlegroups.com
This is definitely the way, only 3 changes I made for it to work: 
  1. added reques object to the response 
  2. set attachment to False 
  3. send image id using vars instead of args urllib quoted, using args gave me too much trouble with slashes and plus signs 

return response.stream(stream, request=request, attachment=False, filename=filename)

{{=URL('serve_image',vars='1234')}}





On Thursday, July 18, 2013 10:58:59 PM UTC-4, Diego Carvallo wrote:

Massimo Di Pierro

unread,
Jul 19, 2013, 4:38:22 AM7/19/13
to web...@googlegroups.com
Would you mind re-post a complete working example for posterity? ;-)
Reply all
Reply to author
Forward
0 new messages