[Zope] Zope newbee fighting with manage_addNewFile

Casey Duncan c.duncan@nlada.org
Wed, 17 Oct 2001 14:07:32 -0400


On Wednesday 17 October 2001 09:14 am, Hans N. Beck allegedly wrote:
> Hi,
>
[snip]
> > >
> > > context.manage_addFile(id, title="test", file=path),
> > >
> > > where "path" is given by  a dtml form. But this only creates files
> > > containing one single string
> > > indentically to "path", nothing to see from the content of the file.
> > > What's going wrong ? Permissions are set correctly, I think.
[snip]
> > > What's going wrong ? Permissions are set correctly, I think.
> >
> > Is your html form using a "file" type input box ?
>
> yes, it is. Now I've tried a script from Zopelab which don't uses
> parameters but use REQUEST object. It seems, that there is a difference:
> in the REQUEST object there is somithing like a "file object".
> (REQUEST[key]=...)Give that to addFile as parameter works fine.
> My variant uses only the path as a simple string, giving it to the
> python
> script as parameter and so to addFile as parameter. In this case the
> created
> file only  contains that string, the path. May be an explanation ?
> What exactly get I back from a form type="file" name="something" ?

two things:

1. make sure your form has attributes enctype="multipart/form-data" and 
method="post" set on it or it will not work.

2. Do not pass the "path" to the file. Zope has no access to the filesystem 
of the client machine so the path is meaningless to Zope. With the enctype 
set, the browser will encode the file data and post it directly to Zope. Zope 
will then decode it into a file object and put it in the REQUEST object. Pass 
this file object (which has the name you gave the input field) to the 
manage_add... method:

e.g.

<form action="addFile" enctype="multipart/form-data" method="post">
  <input name="file_id"><br>
  <input name="file_title"><br>
  <input name="file_data" type="file"><br>
  <input type="submit">
</form>

Then in addFile (a Python Script):

R = context.REQUEST
context.manage_addFile(R.file_id, R.file_title, R.file_data)
R.RESPONSE.redirect('some URL')

hth,
/---------------------------------------------------\
  Casey Duncan, Sr. Web Developer
  National Legal Aid and Defender Association
  c.duncan@nlada.org
\---------------------------------------------------/