FileUpload and Python Script
Can somebody help me on how I can get the filename of a FileUpload object in a python script? When using following code in a python script, context.REQUEST['accessory_image'].filename I get always the following error : Error Type: AttributeError Error Value: 'string' object has no attribute 'filename' It's obvious that I doing something wrong, but what? Thanks Tom.
Tom Deprez wrote:
Can somebody help me on how I can get the filename of a FileUpload object in a python script?
When using following code in a python script,
context.REQUEST['accessory_image'].filename
I get always the following error :
Error Type: AttributeError Error Value: 'string' object has no attribute 'filename'
It's obvious that I doing something wrong, but what?
Thanks Tom.
Hi Tom I don't know, what you would like to do, but maybe I might be able to help with a simple example: A file gets uploaded via a form and then I print the content of the file on the screen. Perhaps this can serve you as some kind of starting point. First create a DTML method with id *uploadForm* and content: <dtml-var standard_html_header> <FORM ACTION="uploadFile" METHOD="POST" ENCTYPE="multipart/form-data"> <INPUT TYPE="file" NAME="file" SIZE="25" VALUE=""><br> <INPUT TYPE="SUBMIT" VALUE="Upload"> <dtml-var standard_html_footer> Then create a python script with id *uploadFile* and content: try: a = context.REQUEST.file.read() print a return printed except KeyError: print '''There was a key error: The script did not get the the field named *file* from your HTML form in *REQUEST* !''' return printed You do *not* have to set any bindings! Just this content in the script ! If you click the *Test* tab for the script you will see the error text, because you didn't privide a key *file* during this test (as this variable comes from the form). Now, call the *uploadForm* method, and you will see the form on your screen. Choose a file on your harddisk (preferably a text or HTML file for this simple example, otherwise you get a funny screen, because the content of the file will get printed) and click the *Upload* button. You should see the content of that file on your screen then. If you would like to see, what your *REQUEST* looks like, you can simply put this code into your script *uploadFile*, which gets called by *uploadForm*: print context.REQUEST return printed HTH --- Flynt
Urgh, thanks, looking at your example, I saw that what I did would normally work. I made a mistake, ie before the user can insert it in the database, I show it as a preview.
From the preview I call the python script... but that's where it was wrong... I transformed it to a string.
I changed the code and now it works. pfew, I think without your message I would have looked over the error everytime. Tom. ----- Original Message ----- From: "Flynt" <rhess@bic.ch> To: "Tom Deprez" <tom.deprez@village.uunet.be> Cc: <zope@zope.org> Sent: Sunday, June 17, 2001 6:01 PM Subject: Re: [Zope] FileUpload and Python Script
Tom Deprez wrote:
Can somebody help me on how I can get the filename of a FileUpload
object in
a python script?
When using following code in a python script,
context.REQUEST['accessory_image'].filename
I get always the following error :
Error Type: AttributeError Error Value: 'string' object has no attribute 'filename'
It's obvious that I doing something wrong, but what?
Thanks Tom.
Hi Tom
I don't know, what you would like to do, but maybe I might be able to help with a simple example: A file gets uploaded via a form and then I print the content of the file on the screen. Perhaps this can serve you as some kind of starting point.
First create a DTML method with id *uploadForm* and content:
<dtml-var standard_html_header> <FORM ACTION="uploadFile" METHOD="POST" ENCTYPE="multipart/form-data"> <INPUT TYPE="file" NAME="file" SIZE="25" VALUE=""><br> <INPUT TYPE="SUBMIT" VALUE="Upload"> <dtml-var standard_html_footer>
Then create a python script with id *uploadFile* and content:
try: a = context.REQUEST.file.read() print a return printed except KeyError: print '''There was a key error: The script did not get the the field named *file* from your HTML form in *REQUEST* !''' return printed
You do *not* have to set any bindings! Just this content in the script ! If you click the *Test* tab for the script you will see the error text, because you didn't privide a key *file* during this test (as this variable comes from the form).
Now, call the *uploadForm* method, and you will see the form on your screen. Choose a file on your harddisk (preferably a text or HTML file for this simple example, otherwise you get a funny screen, because the content of the file will get printed) and click the *Upload* button. You should see the content of that file on your screen then.
If you would like to see, what your *REQUEST* looks like, you can simply put this code into your script *uploadFile*, which gets called by *uploadForm*:
print context.REQUEST return printed
HTH --- Flynt
Tom Deprez writes:
Can somebody help me on how I can get the filename of a FileUpload object in a python script?
When using following code in a python script,
context.REQUEST['accessory_image'].filename
I get always the following error :
Error Type: AttributeError Error Value: 'string' object has no attribute 'filename' "REQUEST['accessor_image'] is a string and not a FileUpload instance.
Are you sure, you followed the preconditions for file upload? * method='post' * enctype='multipart/form-data' * input type=file Dieter
Yes, these are all correct. I found the problem. It was a very stupid thing! See one of previous messages on the list. Tom. ----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Tom Deprez" <tom.deprez@village.uunet.be> Cc: <zope@zope.org> Sent: Sunday, June 17, 2001 9:25 PM Subject: Re: [Zope] FileUpload and Python Script
Tom Deprez writes:
Can somebody help me on how I can get the filename of a FileUpload object in a python script?
When using following code in a python script,
context.REQUEST['accessory_image'].filename
I get always the following error :
Error Type: AttributeError Error Value: 'string' object has no attribute 'filename' "REQUEST['accessor_image'] is a string and not a FileUpload instance.
Are you sure, you followed the preconditions for file upload?
* method='post'
* enctype='multipart/form-data'
* input type=file
Dieter
participants (3)
-
Dieter Maurer -
Flynt -
Tom Deprez