RE: [Zope] Return image from Python Script
How do I return an actual image file from within a Python Script?
return context.restrictedTraverse('/LDML/images/ldml.gif')
Gives me:
<img src="http://127.0.0.1:8080/LDML/images/ldml.gif" alt="" title=""
height="120" width="120" border="0" />
But what I actually want to do is return the file itself. Think Python
Script as symbolic link.
Sorry, must be v. easy but I've been thrashing around on various sites
for about an hour now and I'm getting no closer.
context.REQUEST.RESPONSE.setHeader('content-type',context.image_file.con tent_type) return context.image_file.data BTW, this is a case where file extensions complicate things. Andy
return context.image_file.data
BTW, this is a case where file extensions complicate things.
No it doesn't. It just forces you to write it a little differently. Like so: ################# image = getattr(context, 'image.gif') return str(image.data) ################# jens
On Tuesday 14 December 2004 15:56, Andy Yates wrote:
How do I return an actual image file from within a Python Script?
return context.restrictedTraverse('/LDML/images/ldml.gif')
Gives me:
<img src="http://127.0.0.1:8080/LDML/images/ldml.gif" alt="" title=""
height="120" width="120" border="0" />
But what I actually want to do is return the file itself. Think Python
Script as symbolic link.
Sorry, must be v. easy but I've been thrashing around on various sites
for about an hour now and I'm getting no closer.
context.REQUEST.RESPONSE.setHeader('content-type',context.image_file.con tent_type)
return context.image_file.data
BTW, this is a case where file extensions complicate things.
Andy
isn't the index_html method of OFS.File appropriate? return getattr(context, 'test.png').index_html(context.REQUEST, context.REQUEST.RESPONSE) index_html should also care for setting the Content-Type http-header. Thomas
On 14 Dec 2004, at 14:56, Andy Yates wrote:
How do I return an actual image file from within a Python Script?
return context.restrictedTraverse('/LDML/images/ldml.gif')
Gives me:
<img src="http://127.0.0.1:8080/LDML/images/ldml.gif" alt="" title="" height="120" width="120" border="0" />
But what I actually want to do is return the file itself. Think Python Script as symbolic link.
Sorry, must be v. easy but I've been thrashing around on various sites for about an hour now and I'm getting no closer.
context.REQUEST.RESPONSE.setHeader('content- type',context.image_file.content_type) return context.image_file.data
BTW, this is a case where file extensions complicate things.
Thanks. For the record, my final code was: image_file = context.restrictedTraverse('/LDML/images/ldml.gif') context.REQUEST.RESPONSE.setHeader('content-type', image_file.content_type) return image_file.data Simon Forster
Thanks. For the record, my final code was:
image_file = context.restrictedTraverse('/LDML/images/ldml.gif') context.REQUEST.RESPONSE.setHeader('content-type', image_file.content_type) return image_file.data
That won't work if the file size is large and the data is stored in separate pData structures. You *must* do "return str(image_file.data)" to be on the safe side. jens
On 14 Dec 2004, at 15:55, Jens Vagelpohl wrote:
Thanks. For the record, my final code was:
image_file = context.restrictedTraverse('/LDML/images/ldml.gif') context.REQUEST.RESPONSE.setHeader('content-type', image_file.content_type) return image_file.data
That won't work if the file size is large and the data is stored in separate pData structures. You *must* do "return str(image_file.data)" to be on the safe side.
OK. I've added this - but it does seem counter-intuitive to coerce binary data to a string before returning it. And more to the point, where does one uncover secrets like this? Simon Forster
Thanks. For the record, my final code was:
image_file = context.restrictedTraverse('/LDML/images/ldml.gif') context.REQUEST.RESPONSE.setHeader('content-type', image_file.content_type) return image_file.data
The only proper way is: REQUEST = context.REQUEST image_file = context.restrictedTraverse('/LDML/images/ldml.gif') return image_file.index_html(REQUEST, REQUEST.RESPONSE) This sets the headers correctly. Everything else is a hack. Florent -- Florent Guillaume, Nuxeo (Paris, France) CTO, Director of R&D +33 1 40 33 71 59 http://nuxeo.com fg@nuxeo.com
On 14 Dec 2004, at 16:46, Florent Guillaume wrote:
Thanks. For the record, my final code was:
image_file = context.restrictedTraverse('/LDML/images/ldml.gif') context.REQUEST.RESPONSE.setHeader('content-type', image_file.content_type) return image_file.data
The only proper way is:
REQUEST = context.REQUEST image_file = context.restrictedTraverse('/LDML/images/ldml.gif') return image_file.index_html(REQUEST, REQUEST.RESPONSE)
This sets the headers correctly.
Everything else is a hack.
Florent
Ask 10 people how to do something in Zope - and get 10 different answers! Nothing wrong with hacks. In fact I'd argue that all code is a hack :-) Having said this, the example you gave does feel like the correct way of doing it. I'll change it forthwith. Thanks Simon Forster
participants (5)
-
Andy Yates -
Florent Guillaume -
Jens Vagelpohl -
Simon Forster -
Thomas Schorr