[Zope] uploading a file via an external method

Chris Withers chris at simplistix.co.uk
Tue Jan 18 13:47:22 EST 2005


I'd suggest you look at LocalFS or one of the ExtFile products rather 
than trying to hack something yourself...

Chris

Kate Legere wrote:

> 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>
> 
> _______________________________________________
> 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 )
> 

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope mailing list