How do I ask Zope for the name of an uploaded file?
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.
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@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 )
Cameron Laird wrote at 2003-8-24 17:44 -0500:
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.
When you upload a file via a browser form, the file is encapsulated in a "multipart/form-data" message. The headers describing the file are not in the HTTP request envelop (what you access by "REQUEST.get_header") but in the headers of the part. ZPublisher wraps an uploaded file in a "ZPublisher.HTTPRequest.FileUpload" instance. Its "filename" attribute contains the filename as passed by the browser (usually only the basename without the directory part (exception: broken IE)). Its "headers" attribute contains the headers of the part (of the "multipart") corresponding to the file. Dieter
participants (3)
-
Cameron Laird -
Chris Beaven -
Dieter Maurer