[Zope] Re: [Zope-dev] RE: ZDiscussions

Tony McDonald tony.mcdonald@ncl.ac.uk
Tue, 21 Mar 2000 15:03:19 +0000


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