[Zope] External methods and images

Tony McDonald tony.mcdonald@ncl.ac.uk
Tue, 11 May 1999 09:53:47 +0100


>
> > All I really want to do is pass the name of an image to an external
>> method which then extracts it from a directory, wraps it up in the
>> content-type magic and sends it back to me. Does anyone have
>> something like it working? Any pointers gratefully received.
>
>Well, haven't got it working... haven't even tested it, but my guess would be
>attach the content-type header as plain-text to the front of your 
>return value.
>The syntax of the content-type header should be in any CGI manual. I 
>think that
>when you access an external method directly it acts very like CGI, in that the
>ZPublisher returns it without any preparatory wrapping. So I think you need to
>put the header in manually. Good luck.

Thanks for the thoughts.

I've got something very similar to that, in that at the top level of 
the Zope tree, I have an external method called 'img'. The code is 
here;
def img(self, src, RESPONSE):
         import os
         img_dir = '/home/zope/Zope-1.11.0pr1-src/Images'
         typemap={'.gif' : 'image/gif', '.jpg' : 'image/jpeg'}

         img_name=os.path.join(img_dir, src)

         # Determine the ad type
         # img_type=self.image_type(img_name)
         img_ext = img_name[-4:]
         img_type = typemap[img_ext]    

         # Open ad file, using 'rb' to open it in binary mode!
         img_file=open(img_name, 'rb')
         img_data=img_file.read()
         img_file.close()

         # Set the content-type of the response
         RESPONSE.setHeader('content-type', img_type)
         return img_data

The Images directory has a file in it called s1cb.gif

If I access the image so
http://server/img?src=s1cb.gif
I get an image on the browser.

If I use the following DTML,
<!--#var "img(src='s1cb.gif')"-->
I get a TypeError. I also get it with <!--#var "img('s1cb.gif')"-->

It gets weirder though...
If I use <!--#var "img('s1cb_p0000001.gif', RESPONSE)"--> however, I 
get a document downloaded to the desktop that has rendered HTML *and* 
what looks like binary data (it's certainly got GIF89A embedded in 
it) where the DTML was placed.

Ok, so what am I doing wrong?
(answers on a postcard please)
TIA
tone
------
Dr Tony McDonald,  FMCC, Networked Learning Environments Project
The Medical School, Newcastle University Tel: +44 191 222 5888
Fingerprint: 3450 876D FA41 B926 D3DD  F8C3 F2D0 C3B9 8B38 18A2