quick search:
 

Upload a File or Image

Submitted by: runyaga
Last Edited: 2004-10-14

Category: DTML

Average rating is: 3.0 out of 5 (2 ratings)

Description:
for some reason, people make it hard on themselves when uploading a file in ZOPE. Its quite simple. This shows you too ways of saving
the file - on in DTML and the other in a Script (Python). This will only work on versions of ZOPE w/ Python 2.1 or above.


Source (Text):
#create a DTML Method called, uploadForm
<form method="POST" action="processForm" enctype="multipart/form-data">
  Id: <input type="text" name="id" value="" /> <br />
  Title: <input type="text" name="title" value="" /> <br />
  File or Image: <input type="file" name="file" value="" /> <br />
  <input type="submit" name="save" value="Upload" />
</form>

#Way 1.  The DTML Route
#create a DTML Method called processForm
<dtml-call "REQUEST.set('file_type', file.headers['Content-Type'] )">
<dtml-if "file_type.find('image')!=-1">
    <dtml-call "this().manage_addImage(REQUEST['id'], file, REQUEST['title'])">
<dtml-else>
    <dtml-call "this().manage_addFile(REQUEST['id'], file, REQUEST['title'])">
</dtml-if>
Finished

#Way 2. The Python Route
#in Script (Python), called processForm
#parameters: id, file, title=''
REQUEST=context.REQUEST
content_type=file.headers['Content-Type']
if content_type.find('image')!=-1: #.find() is a method in Python2.0<
    context.manage_addImage(id, file, title)
else:
    context.manage_addFile(id, file, title)
return 'Finished'

Explanation:
Very straight forward. Two ways, IMHO The Script(Python) is very clear.
There is a caveat (please correct me if I'm wrong). If your DTML Method
or your Script(Python) are not in a Folder or something that derives from
ObjcetManager you dont get manage_addXXXXX methods available. so you
would have to manage_addProduct['OFSP'].manage_addXXXXX.

I'm not quite sure if this is true or not, but for most 99% of the people this
script encompesses most of what they want. instead of returning a string,
you may want to return str(REQUEST)


Comments:

See also: by pieterb2 - 2004-10-14
http://www.zopelabs.com/cookbook/1029932854