[Zope] Uploading image files via forms ...
Darran Edmundson
Darran.Edmundson@anu.edu.au
Sun, 13 Feb 2000 02:18:17 +1100
Imagine a ZClass that can optionally contain an
image. You'll probably have something like the following
in your addForm method:
<INPUT TYPE="file" NAME="newimage" VALUE="">
In your ZClass add constructor, after the new instance is
created, we want to (conditionally) create the image only
if a file was chosen. How? A piece of code like the
following doesn't work:
<dtml-if newimage>
<dtml-call "manage_addImage('id', newimage.read(), '')">
</dtml-if>
When no file is chosen, Zope still creates an image (size 0)
because REQUEST.newimage is defined (albeit empty). I
immediately tried
<dtml-if "_.len(_.str(newimage)) !=0">
<dtml-call "manage_addImage('id', newimage.read(), '')">
</dtml-if>
but this still creates a size 0 image. Why?
Well, there's yet another possible way out of this mess
as documented in the How-to
"http://www.zope.org/Members/Zen/howto/FormVariableTypes"
<INPUT TYPE="file" NAME="newimage:string:ignore_empty" VALUE="">
with
<dtml-if "newimage !=''">
<dtml-call "manage_addImage('id', newimage, '')">
</dtml-if>
(Note, the string typecast is necessary or else ignore_empty
seems to have no effect.) This works in that not choosing
a file stops the image from getting created and an image
does get created if one was chosen. The problem now is
that the string typecast has lost the content_type of the
uploaded image. If it was "image/gif", it now shows up
as a raw octet_stream.
If anyone can provide a way out of this morass, I'd greatly
appreciate it.
Cheers,
Darran "veteran newbie" Edmundson