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

John Schinnerer johnschinnerer at yahoo.com
Sat Mar 27 02:26:46 EST 2004


Hello,

OK, thanks...I am not using plone or CMF in this case, so this script
does not work for me.  At least I assume not since I see what look like
calls to plone and CMF methods.

Are you answering my other question, about how to change the data of an
existing image file?  If so, the 'delete then add' I do now seems much
simpler...at least, it works, and is two lines of code...?

My main need now is the below question - how to test if there is a file
to be uploaded or not by checking in REQUEST somehow.

> >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 looked 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?

> 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.')
> 


__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html



More information about the Zope mailing list