[Zope] How do I test for file to upload (or not) via REQUEST data?

robert rottermann robert at redcor.ch
Sat Mar 27 02:06:59 EST 2004


John Schinnerer wrote:

>Hello,
>
>I need to test form data passed in REQUEST to see if there is or is not
>a file specified to upload.
>The form element is
>
><input type="file" name="image_file" size="35" value="" />
>
>I tried testing for an empty field like with string and text variables,
>like so:
>
>if REQUEST.image_file != '':
>
>But that always seemed to be true even when I left the form field
>blank.
>So I lookd at REQUEST and it shows this:
>
>image_file    <ZPublisher.HTTPRequest.FileUpload instance at 0x89ef9d4>
>
>That's new to me...
>How do I reference the string in that field to test if it's empty or
>not, *or* do some equivalent test that tells me there is or is not a
>file specified to upload?
>
>Thanks,
>John S.
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Finance Tax Center - File online. File on time.
>http://taxes.yahoo.com/filing.html
>
>_______________________________________________
>Zope maillist  -  Zope at zope.org
>http://mail.zope.org/mailman/listinfo/zope
>**   No cross posts or HTML encoding!  **
>(Related lists - 
> http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )
>
>
>  
>
Here is the file_edit.py script from plone that shows how to do it
HTH
Robert
## Controller Python Script "file_edit"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=precondition='', file='', id='', title=None, 
description=None, file_data=''
##title=Edit a file
##

from StringIO import StringIO

original_id=context.getId()
filename=getattr(file,'filename', '')

if file and filename and context.isIDAutoGenerated(original_id):
#   if there is no id or an autogenerated id, use the filename as the id
#   if not id or context.isIDAutoGenerated(id):
#   if there is no id, use the filename as the id
    if not id:
        id = filename[max( string.rfind(filename, '/')
                       , string.rfind(filename, '\\')
                       , string.rfind(filename, ':') )+1:]
if file and hasattr(file, 'seek'):
    file.seek(0)

# if there is no id specified, keep the current one
if not id:
    id = context.getId()

if not file and file_data:
    file=StringIO(file_data)

new_context = context.portal_factory.doCreate(context, id)

new_context.plone_utils.contentEdit( new_context
                                   , id=id
                                   , title=title
                                   , description=description )
new_context.edit( precondition=precondition, file=file )

from Products.CMFPlone import transaction_note
transaction_note('Edited file %s at %s' % (new_context.title_or_id(), 
new_context.absolute_url()))

return state.set(context=new_context, portal_status_message='File 
changes saved.')




More information about the Zope mailing list