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?
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=file.filename title=filename id=filename[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 ----- From: Mark Spencer To: zope@zope.org 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?
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="post" action="Create_new_zope_file_object" enctype="multipart/form-data"> Path and File: <input type="file" name="new_file_object"> <input type="submit" value="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=new_file_object.filename title=filename id=filename[max(string.rfind(filename, '/'), string.rfind(filename, '\\'), )+1:] if(new_file_object): container.manage_addImage(id, title, new_file_object) ----- Original Message ----- From: Robert Rottermann To: Mark Spencer ; zope@zope.org 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=file.filename title=filename id=filename[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 ----- From: Mark Spencer To: zope@zope.org 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?
There is an example coded in ZPT/Python scripts in the newest Zope releases "Examples" folder of a file managment utility. ----- Original Message ----- From: Mark Spencer To: zope@zope.org Sent: Sunday, March 24, 2002 8:46 PM 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?
participants (3)
-
Chris McDonough -
Mark Spencer -
Robert Rottermann