[Zope] Re: File Upload via form (with other form variable) and storing it in a database

Maik Jablonski maik.jablonski@uni-bielefeld.de
Mon, 03 Mar 2003 09:17:55 +0100


Edward Muller wrote:
> Okay... Saw that 
> 
> But it doesn't tell me how to get the file into a postgres database
> though.

Take the content of the file and store it with an ExternalMethod
  in a BLOB-field... btw: I don't think that Postgres is a good storage 
for binary-objects...

# insertFile.py
# from 
http://www.dzug.org/mailinglisten/zope/archive/2002/2002-11/1037545798840

import psycopg

def add_fileobject(self, obj_id, title, file, REQUEST=None):
     " insert binary file into postgres sql table "

     # open a database connection
     o = psycopg.connect("dbname=myDB user=postgres")

     # create cursor
     c = o.cursor()

     c.execute("INSERT INTO TBL_FILES (obj_id, imagetitle, imagedata) VALUES
(%(obj_id)i, %(img_title)s, %(data)s)",
                {'obj_id':obj_id, 'img_title':title,
'data':psycopg.Binary(file.read())}
              )

     # commit on the database connection
     o.commit()

     if REQUEST is not None:
         REQUEST.RESPONSE.redirect('index_html')


Cheers, Maik
-- 
Deutsche/German Zope User Group (DZUG)
http://www.dzug.org/Members/mjablonski/