0

I am using 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 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?
4

1 回答 1

2

这比那更容易。

首先你创建一个像这样的动作:

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)

然后在您看来,您可以:

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

其中 1234 是您想要的图像的 id。

于 2013-07-17T09:14:38.013 回答