[Zope] External methods and images

Martijn Pieters mj@antraciet.nl
Tue, 11 May 1999 15:01:52 +0200


At 10:53 11/05/99 , Tony McDonald wrote:
>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)

Your external method returns the binary image, while the <!--#var--> tag 
expects data it can include into a _textual_ document. DTML Methods and 
Documents should return a textual format, not binairy image data.

To include an image into a HTML document, you can best give the URL of your 
external method to the browser, like so:

<IMG SRC="/img?src=s1cb.gif">

The browser will then request that URL from the server, and will get the 
image, wich it will place into it's proper place in the rendered HTML document.


--
Martijn Pieters, Web Developer
| Antraciet http://www.antraciet.nl
| Tel: +31-35-6254545 Fax: +31-35-6254555
| mailto:mj@antraciet.nl http://www.antraciet.nl/~mj
| PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149
------------------------------------------