[Zope-DB] Displaying an image from a MySQL blob

Aaron Fosdick aaron at hypervision.com
Thu Jul 1 16:57:12 EDT 2004


Hi - I've been battling this for a couple of days, and am hoping someone 
here can help.  I want to zope to display an image out of a MySQL blob 
field.  While uploading images works, displaying them has me stumped. I 
see that a number of other people are running into this issue also.

SQL method: searchImage:
    select filetype, picture from image where
    <dtml-sqltest pic_ID op="eq" type="int">

Python Script: displayImage:
    photo = context.searchImage(pic_ID = pic_ID)
    container.REQUEST.RESPONSE.setHeader('Content-type', 'image/' + 
photo[0].filetype)
    return photo[0].picture

Ideally, something like: <img src ="./displayImage?pic_ID=5"> would  
serve up a jpeg.

I've followed Murphy's how-to for "Upload, Insert and Retrieval of 
BLOBs", but that doesn't work since the Content-Type doesn't match the 
page content - which is text containing escaped data. This is the same 
result as 'test' in the SQL method:
    array('c', '\xff\xd8\xff......')
This wouldn't work anyway as I understand that zope security won't allow 
you to serve non-html from a python script.


So there are three options that I've seen and that's where I'm getting 
stuck.  From an efficiency perspective, which of the below is the best 
method and what would the code look like?
    1. External method  - in the python script above, use:  return 
context.extMethod(photo[0].picture)
        This calls the method and converts the array into data
    2. External method as a brain - pass the query result and process it 
there.
    3. Don't use the displayImage python script at all. Do everything in 
the external method, including the sql query.
        Is there any way to just get the raw data into a string in 
non-escaped form? I imagine that would be the least costly.

I've been working on the first method, but can't seem to get a result as 
anything other than text or elements of the array.

Thanks for any assistance

--Aaron




More information about the Zope-DB mailing list