[Zope] How to upload files and create file objects from DTML page?
Mark Spencer
markwspencer@bigpond.com
Mon, 25 Mar 2002 23:43:18 +1100
This is a multi-part message in MIME format.
------=_NextPart_000_002D_01C1D456.D7608EF0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Thank you for your help Robert,
unfortunately I can't get the code to work. It adds an image object with =
the correct Content Type, but the Title field reads =
<ZPublisher.HTTPRequest.FileUpload instance at 121a858> and no image =
appears to be present (it says size is ~92bytes).
My form code is:
<form method=3D"post" action=3D"Create_new_zope_file_object" =
enctype=3D"multipart/form-data">
Path and File: <input type=3D"file" name=3D"new_file_object">
<input type=3D"submit" value=3D"Upload this file to Library">
</form>
My Python script parameter is:
new_file_object
My Python Script Code is:
import sys, string, os
if hasattr(new_file_object,'filename'):
filename=3Dnew_file_object.filename
title=3Dfilename
id=3Dfilename[max(string.rfind(filename, '/'), =
string.rfind(filename, '\\'), )+1:]
=20
if(new_file_object):
container.manage_addImage(id, title, new_file_object)
----- Original Message -----=20
From: Robert Rottermann=20
To: Mark Spencer ; zope@zope.org=20
Sent: Monday, March 25, 2002 5:47 PM
Subject: Re: [Zope] How to upload files and create file objects from =
DTML page?
in the method (probably a python script) dealing with the upload use =
the following
I patched it from several of my methods. It is not tested but it =
should give you the idea
Robert
Call this function as a the action of a file upload form.
If it is in a python script just leave out the def part, and give the =
script the
parameter file.
def manage_addMyFile ( self, file ) :
""" add file """
import os
if hasattr(file,'filename'):
filename=3Dfile.filename
title=3Dfilename
id=3Dfilename[max(string.rfind(filename, '/'),
string.rfind(filename, '\\'),
string.rfind(filename, ':'),
)+1:]
if(file):
if(os.path.splitext(filename)[1] in ['.jpg', '.gif']):
self.manage_addImage(id, title, file)
if other extention ....
----- Original Message -----=20
From: Mark Spencer=20
To: zope@zope.org=20
Sent: Monday, March 25, 2002 2:46 AM
Subject: [Zope] How to upload files and create file objects from =
DTML page?
I want to have a normal DTML page for users that allows them to =
upload and download files (without needing to use the management =
interface). The downloading and display of files is fine, but I cant =
figure out the zope/python code to create a new file object and store an =
uploaded file. The files could be upto ~60MB in size, and can be zip or =
doc files etc. Has anyone seen suitable sample code for this?
------=_NextPart_000_002D_01C1D456.D7608EF0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2713.1100" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial>Thank you for your help Robert,</FONT></DIV>
<DIV><FONT face=3DArial>unfortunately I can't get the code to work. =
It adds=20
an image object with the correct Content Type, but the Title =
field reads=20
<ZPublisher.HTTPRequest.FileUpload instance at 121a858> and no =
image=20
appears to be present (it says size is ~92bytes).</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial>My form code is:</FONT></DIV>
<DIV><FONT face=3DArial> <form method=3D"post"=20
action=3D"Create_new_zope_file_object"=20
enctype=3D"multipart/form-data"><BR> Path and File: =
<input=20
type=3D"file" name=3D"new_file_object"><BR> =
<input=20
type=3D"submit" value=3D"Upload this file to =
Library"><BR> =20
</form><BR>My Python script parameter is:</FONT></DIV>
<DIV><FONT face=3DArial> new_file_object</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial>My Python Script Code is:</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial> import sys, string, =
os</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial> if=20
hasattr(new_file_object,'filename'):<BR> =
=20
filename=3Dnew_file_object.filename<BR> =
=20
title=3Dfilename<BR> =20
id=3Dfilename[max(string.rfind(filename, '/'), string.rfind(filename, =
'\\'),=20
)+1:]<BR> <BR> =
if(new_file_object):<BR> =20
container.manage_addImage(id, title,=20
new_file_object)<BR></FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Drobert@redcor.ch href=3D"mailto:robert@redcor.ch">Robert =
Rottermann</A>=20
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dmarkwspencer@bigpond.com=20
href=3D"mailto:markwspencer@bigpond.com">Mark Spencer</A> ; <A=20
title=3Dzope@zope.org href=3D"mailto:zope@zope.org">zope@zope.org</A> =
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Monday, March 25, 2002 =
5:47=20
PM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [Zope] How to =
upload files=20
and create file objects from DTML page?</DIV>
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><FONT=20
face=3DArial></FONT><FONT face=3DArial></FONT><FONT =
face=3DArial></FONT><FONT=20
face=3DArial></FONT><FONT face=3DArial></FONT><FONT =
face=3DArial></FONT><BR></DIV>
<DIV><FONT face=3DArial size=3D2>in the method (probably a python =
script) dealing=20
with the upload use the following</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I patched it from several of my =
methods. It is=20
not tested but it should give you the idea</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Robert</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Call this function as a the action of =
a file=20
upload form.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>If it is in a python script just =
leave out the=20
def part, and give the script the</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>parameter file.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> def =
manage_addMyFile ( self,=20
file ) :<BR> """ add file=20
"""<BR> import=20
os<BR> if=20
=
hasattr(file,'filename'):<BR> &n=
bsp; =20
=
filename=3Dfile.filename<BR> &nb=
sp; =20
=
title=3Dfilename<BR>  =
; =20
id=3Dfilename[max(string.rfind(filename,=20
=
'/'),<BR> &nbs=
p;  =
; =20
string.rfind(filename,=20
=
'\\'),<BR> &nb=
sp; &nbs=
p; =20
string.rfind(filename,=20
=
':'),<BR> &nbs=
p;  =
; =20
)+1:]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2> =20
=
if(file):<BR> =
=20
if(os.path.splitext(filename)[1] in ['.jpg',=20
=
'.gif']):<BR> =
=20
self.manage_addImage(id, title,=20
=
file)<BR> &nbs=
p; if=20
other extention ....<BR></DIV></FONT>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dmarkwspencer@bigpond.com=20
href=3D"mailto:markwspencer@bigpond.com">Mark Spencer</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3Dzope@zope.org=20
href=3D"mailto:zope@zope.org">zope@zope.org</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Monday, March 25, 2002 =
2:46=20
AM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Zope] How to upload =
files and=20
create file objects from DTML page?</DIV>
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><BR></DIV>
<DIV><FONT face=3DArial>I want to have a normal DTML page for users =
that=20
allows them to upload and download files (without needing =
to use=20
the management interface). The downloading and display of files is =
fine, but=20
I cant figure out the zope/python code to create a new file =
object and=20
store an uploaded file. The files could be upto ~60MB in size, =
and can=20
be zip or doc files etc. Has anyone seen suitable sample code for=20
this?</FONT></DIV></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_002D_01C1D456.D7608EF0--