How do I test for file to upload (or not) via REQUEST data?
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
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@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.')
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
From: "John Schinnerer" <johnschinnerer@yahoo.com>
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.
If you use the standard html input tag to get the user to select a file eg. <input type="file" name="fname"> Then what you see in REQUEST is 'fname' as a 'fileupload instance' (not a file name). 'fname' has an attribute called 'filename', which you can use as follows: <dtml-call "REQUEST.set('title', fname.filename)"> <dtml-call "manage_addFile(id=REQUEST['id'], file=REQUEST['fname'], title=REQUEST['title'])"> HTH Jonathan
Hello,
If you use the standard html input tag to get the user to select a file
eg. <input type="file" name="fname">
Then what you see in REQUEST is 'fname' as a 'fileupload instance' (not a file name). 'fname' has an attribute called 'filename'...
Thanks! That was just the info I needed. In my case it's in python: # if filename not empty, replace current image file if REQUEST.image_file.filename != '': self.manage_delObjects(...blah...) self.manage_addImage(...blah blah blah...) thanks again, John S. __________________________________ Do you Yahoo!? Yahoo! Finance Tax Center - File online. File on time. http://taxes.yahoo.com/filing.html
John Schinnerer wrote:
Thanks! That was just the info I needed. In my case it's in python:
good man!
# if filename not empty, replace current image file if REQUEST.image_file.filename != '': self.manage_delObjects(...blah...) self.manage_addImage(...blah blah blah...)
...slight refinement for some browsers: file = REQUEST.image_file if file and file.filename != '': self.manage_delObjects(...blah...) self.manage_addImage(...blah blah blah...) cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (4)
-
Chris Withers -
John Schinnerer -
robert rottermann -
Small Business Services