I'm trying to export some files generated from my application and
provide them as a zip file to the user. I'm creating a zip file using
Python zipfile module and the zip file is created fine. Then I upload
that file to the DB to allow them to download it from the usual
uploads directory. The code snippet below works fine and a file is
uploaded, but the file is corrupted somehow.

db.py
    db.define_table("exportitem",
        SQLField("org_id", db.organisations, notnull=True,
writable=False, default=session.org_id),
        SQLField("app_id", "integer", default=session.app_id,
notnull=True),
        SQLField("proj_id", "integer", default=session.proj_id,
notnull=True),
        SQLField("title", "string", length=50, notnull=True,
label="Title"),
        SQLField("created", "datetime", notnull=True,
default=datetime.today(), label="Created", writable=False),
        SQLField("created_by", db.auth_user, notnull=True,
default=session.user_id, writable=False),
        SQLField("exportfile", "upload", notnull=True),
        migrate=migrate,fake_migrate=fake_migrate)

testzip()
    fname=''.join((str(session.org_id),str(datetime.today())))
    my_crypt = CRYPT(key=auth.settings.hmac_key)
    zname = ''.join((my_crypt(fname)[0],'.zip'))

 
zfile='/'.join((request.env.web2py_path,'applications',request.application,'temp',zname))
#
# zipfile created here in temp directory
#
....
#
#   now store the file in the database.
#
    zid=db.exportitem.insert(title='testing the zip file
generator',exportfile=db.exportitem.exportfile.store(open(zfile,'rb')))

The file stored in the uploads directory is corrupted - and not
identical to the original of course.

Am I missing something?

Reply via email to