Q. How do you add a file to a Zope object?
A. Make the object folder-like and drop a File object in there. This way you can make it an Image instead if you wish, or any other Zope object your heart feels like enabling (if you subclass ObjectManager instead of Folder, you can choose which classes of sub-objects are acceptable) and support more than one attachment easily.
Well, I don't think that is the optimum solution, especially for big sites. Even though that is the Zope way of doing things, I hate the idea that I clutter my ZODB with files. I would suggest you add a property called attachments to your Class which points to various URLs and then serve the files via Apache. You may want to use PCGI to do this. We just switched to PCGI and now we serve Images and Documents from another port via Apache (we can also use SSL now). That helps to keep the I/O stream to the ZODB file moderate. Remember, ZODB is just big one file (even though Zope does a lot of caching). You could also check into LocalFS, which is nice in some cases (even though we were unsuccessful to upload files, using it.) Regards, Stephan -- Stephan Richter - (901) 573-3308 - srichter@cbu.edu CBU - Physics & Chemistry; Framework Web - Web Design & Development PGP Key: 735E C61E 5C64 F430 4F9C 798E DCA2 07E3 E42B 5391
At 8:28 am -0600 21/3/00, Stephan Richter wrote:
You could also check into LocalFS, which is nice in some cases (even though we were unsuccessful to upload files, using it.)
Here's something that worked for me, it's got extraneous imports and stuff ('cos it's the front end of something bigger), but it does work. I add nnn_ to the beginning of file names so I can have more than one file called 'test.doc' in the directory, and I've not finished the URL stuff yet, but it certainly uploads normal files to LocalFS (probably because it bypasses the LocalFS machinery all together ? :) . add_details is a ZSQL method that adds info about the upload into a database. ----- <form method="post" action="upload_file" enctype="multipart/form-data"> <input type=file name="file"> <input type=text name="url"> </form> ----- import os, sys, string, mimetypes, stat import DocumentTemplate from Acquisition import Implicit from OFS.Image import File from OFS.content_types import guess_content_type savedir = "/home/zope/UPLOAD_FILES" split = string.split(file.filename, '\\') thefilename = split[-1] nxtfile = len(os.listdir(savedir)) +1 okay = 0 if file.read(1) == '': if url: oname = os.path.join(savedir, "%s_" % nxtfile) f = open(oname, "wb") f.write(url) f.close() content_type = 'url' filesize=os.stat(oname)[6] thefilename = url okay = 1 results = "Got a URL [%s]" % url else: file.seek(0) oname = os.path.join(savedir, "%s_" % nxtfile + thefilename) f = open(oname, "wb") body=file.read() f.write(body) f.close() content_type, enc = guess_content_type(oname, body, None) filesize=os.stat(oname)[6] okay = 1 results = "File saved %s, size (bytes) %s" % (thefilename, filesize) if okay: self.add_details(nxtfile=nxtfile,filename=thefilename,filesize=filesiz e,content_type=content_type) else: results = "No file was specified" template_str = self.index_html.read_raw() template = DocumentTemplate.HTML(template_str) theresult = template(self, results=results) return theresult It's not quite production quality (need to fix the URL stuff), but it works nicely with LocalFS. hth tone ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
participants (2)
-
Stephan Richter -
Tony McDonald