Hi Alan. Seeing your code must have helped because I did get it to run. 

The following shows a demo script with functions to upload a file and 
download a file utilizing a table containing a blob field. It worked twice 
on a pdf for me. Note that I had to change the filename type from 'upload' 
to 'string' in order to bypass an error message in the 
dal._attempt_upload(fields).

# -*- coding: cp1252 -*-
import os, sys
from gluon import DAL, Field, fileutils
from easygui import fileopenbox, diropenbox

db = DAL('oracle://...',
         auto_import=True)  ## Still playing with this one to try and avoid 
re-defining table?

db.define_table('wiki_media',
                Field('filename', 'upload', uploadfield='filedata'),
                Field('title', 'string'),
                Field('filedata', 'blob'),
                migrate=False, fake_migrate=True)

def upload_file():
    """demonstrate uploading a file to database programatically,
    """
    stream=None
    file_path=fileopenbox(msg='Pick a file', title='Picking files', 
default=None)
    if file_path:
        print 'file_path= ' + file_path
        stream = open(file_path, 'rb')
    else:
        print 'file_path undefined'
        return None
    
    path,filename = os.path.split(file_path)
    #workaround to possible suprise bug in web2py?
    db.wiki_media.filename.type='string'
    
file_id=db.wiki_media.insert(filename=db.wiki_media.filename.store(stream, 
filename=filename, path=path), filedata=stream.read())
    db.commit()
    stream.close()
    if file_id:
        print 'file_id=' + str(file_id)
    return file_id


def download_file(file_id, table='wiki_media'):
    """ In this demo will download the file with the given file_id from the
        given table. Table must have a filename field of type upload and a
        filedata field of type blob
    """
    table = db[table]
    if file_id:
        print 'file_id=' + str(file_id)
    else:
        print 'file_id is empty'
        return None
    
    directory=diropenbox(title='Select destination directory')
    
    record = table(table.id==file_id)
    (filename, stream) = table.filename.retrieve(record.filename)
    pathname = os.path.join(directory,filename)

    import shutil           #In fileutils.py the developer mentioned he 
suspects a bug in shutil.copyfileobj
                            #but I didn't know how to get the length of a 
cStringIO object to 
                            #use fileutils.copystream()

    shutil.copyfileobj(stream,open(pathname,'wb'))

    stream.close()

    return


Hope it helps someone.

-Bill



-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to