[Zope] How do I ask Zope for the name of an uploaded file?

Chris Beaven chris at d-designz.co.nz
Mon Aug 25 14:15:16 EDT 2003


It's a property of the file_upload object.

So if you had: <input type="file" name="file">
Then you'd do something like: context.REQUEST['file'].filename

You will also probably want to strip the filename to get just the name 
not the whole path. I do this through an external method which uses 
os.path.basename

I also have this script to check if the file is empty (returns 
file_upload object unless it's empty):

## Script (Python) "check_file"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=file
##title=Returns file [except empty files which return None]
##
# A file object may exist even though the file is empty,
#   this script returns the file or None if it's empty

# try to read a single byte from file
# empty files return empty string at first read
if file.read(1) == "":
  return None
else:
  # go back to start of file
  file.seek(0) 
  return file


HTH
Chris Beaven


Cameron Laird wrote:

>I'll abbreviate this:  in a Python script that processes a
>file upload, how do I express the name of the uploaded file--
>that is, the name of the file the user selected with his 
>browser?  I expected I'd find this in something like
>  context.REQUEST.get_header('CONTENT-DISPOSITION')
>or
>  context.REQUEST.get_header('filename')
>or somesuch, but I'm just getting None from those.
>
>Note that this information is available only through the 
>HTTP dialogue; as near as I can tell, it is NOT available
>through the context.REQUEST dictionary.
>
>I anticipate a couple of possibilities.  One is that some-
>one will know immediately what I'm trying to say, and will
>give me the one-line answer I'm after.
>
>The second is that my description puzzles everyone.  In 
>that case, I'll happily detail a model that should make all
>this clear.  I prefer to avoid the work, though, if it's un-
>necessary.
>
>_______________________________________________
>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 )
>
>  
>




More information about the Zope mailing list