[Zope] uploading a file via an external method
Kate Legere
klegere at kfpl.ca
Wed Jan 12 13:17:00 EST 2005
I've used this to upload files to a specific directory, not to Zope (or as
an Zope object). The problem is that it's looking in the folder for the
files and I'd like it to upload from a chosen directory (or my desktop) in
the same way as manage_addFile does.
Kate
external method called upload_external:
import os, shutil
def upload_external(self,file,REQUEST):
#check if file was sent correctly
if file.read(1) == '':
return '<h2>File does not exist</h2>'
else:
file.seek(0)
iname=os.path.basename(file.filename)
oname=os.path.join(self.savedir,iname)
f=open(oname,"wb")
f.write(file.read())
# let's get the mime information
mime_type=file.headers.headers[1]
user=str(REQUEST.AUTHENTICATED_USER)
try:
self.add_entry(path=self.savedir,filename=iname,owner=user)
except:
os.remove(oname)
return '<h2>File upload not completed correctly</h2>'
return '''<h2>%s was sent correctly, and saved in %s</h2><h3>The Mime Type
of the file is</h3>%s<h3>Saved by</h3>%s''' % (iname,oname,mime_type,user)
The self.savedir is a property set in the folder which contains the external
method and tells the external method where to put the file.
The self.add_entry is an update on a database to keep track of the files
uploaded, etc.
----------
upload_html:
<FORM ACTION="upload_external" METHOD="POST"
ENCTYPE="multipart/form-data">
<TABLE CELLSPACING="2">
<TR>
<TH ALIGN="LEFT" VALIGN="TOP">File</TH>
<TD ALIGN="LEFT" VALIGN="TOP">
<INPUT TYPE="file" NAME="file" SIZE="25" VALUE="">
</TD>
</TR>
<TR>
<TD></TD>
<TD><BR><INPUT TYPE="SUBMIT" VALUE="Change"></TD>
</TR>
</TABLE>
</FORM>
More information about the Zope
mailing list