[Zope] FileUpload and Python Script

Flynt rhess@bic.ch
Sun, 17 Jun 2001 18:01:36 +0200


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