import os, sysfrom gluon import DAL, Fieldfrom easygui import fileopenbox, diropenbox
db = DAL('oracle://...', auto_import=True)db.define_table('wiki_media', Field('wiki_page', 'reference wiki_page'), Field('title', required=True), Field('filename', 'upload', required=True), Field("filedata", "blob"), migrate=False)
db.wiki_media.filename.uploadfield='filedata'
file_id = None
def upload_file(): """demonstrate uploading a file programmatically, """ stream=None file_path=fileopenbox(msg='Pick a file', title='Choose file', default=None) if file_path: print 'file_path= '.join([file_path]) stream = open(file_path, 'rb') else: return None file_id=db.wiki_media.insert(filename=db.wiki_media.filename.store(stream,file_path), filedata=stream.read()) stream.close() return file_idMicrosoft Windows [Version 6.1.7600]Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\bthayer>cd %WEB2PY_HOME%
C:\web2py-1045bab06391\web2py-1045bab06391>cd appl*/TAMOTO
C:\web2py-1045bab06391\web2py-1045bab06391\applications\TAMOTO>cd private
C:\web2py-1045bab06391\web2py-1045bab06391\applications\TAMOTO\private>pythonPython 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import test_tamoto_rpc as trpc>>> trpc.upload_file()C:\Users\bthayer\Documents\car\carfiles.txtTraceback (most recent call last): File "<stdin>", line 1, in <module> File "test_tamoto_rpc.py", line 33, in upload_file filedata=stream.read()) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8276, in insert self._attempt_upload(fields) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8258, in _attempt_upload raise RuntimeError("Unable to handle upload")RuntimeError: Unable to handle upload>>>I am attempting to write a function that will eventually go in to a migration script to upload legacy data files. As you can see I
Replacing the store call with this should avoid the error:
file_id=db.wiki_media.insert(filename=db.wiki_media.filename.store(stream, filename=<a file name>, path=file_path)
>>> import test_tamoto_rpc as trpc>>> trpc.upload_file()C:\Users\bthayer\Documents\car\carfiles.txtwiki_media.filedatawiki_media.cell_idwiki_media.lib_idwiki_media.layout_idwiki_media.wiki_pagewiki_media.titlewiki_media.meas_idwiki_media.filenamefilenamewiki_media.filename.9b724ad17775d60e.63617266696c65732e747874.txtTraceback (most recent call last): File "<console>", line 1, in <module> File "C:\web2py-1045bab06391\web2py-1045bab06391\applications\TAMOTO\private\test_tamoto_rpc.py", line 38, in upload_file filedata=stream.read()) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8280, in insert self._attempt_upload(fields) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8262, in _attempt_upload raise RuntimeError("Unable to handle upload")RuntimeError: Unable to handle upload>>>(InteractiveConsole)>>> import test_tamoto_rpc as trpc>>> trpc.upload_file()C:\Users\bthayer\Documents\car\carfiles.txtwiki_media.filedatawiki_media.cell_idwiki_media.lib_idwiki_media.layout_idwiki_media.wiki_pagewiki_media.titlewiki_media.meas_idwiki_media.filenamefilenamewiki_media.filename.a8857e792725cc9c.63617266696c65732e747874.txtTraceback (most recent call last): File "<console>", line 1, in <module> File "C:\web2py-1045bab06391\web2py-1045bab06391\applications\TAMOTO\private\test_tamoto_rpc.py", line 38, in upload_file filedata=stream.read()) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8281, in insert self._attempt_upload(fields) File "C:\web2py-1045bab06391\web2py-1045bab06391\gluon\dal.py", line 8258, in _attempt_upload new_name = field.store(value.file,filename=value.filename)AttributeError: 'unicode' object has no attribute 'file'>>>
Your code also threw the same error so before I un-commented the if statements in dal.py I added some print statements
# -*- coding: cp1252 -*-import os, sysfrom gluon import DAL, Fieldfrom gluon.tools import Authfrom gluon.validators import IS_SLUGfrom easygui import fileopenbox, diropenbox
db = DAL('oracle://<user>/<pwd>@<server>:1521', auto_import=True)file_id = Nonedef upload_file(): """demonstrate uploading a file to tomoto programatically.
this will only work for files accessible from the local machine. """ 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: return None blobdoc = stream.read() path,filename = os.path.split(file_path) #workaround to possible surprise bug in web2py? db.wiki_media.filename.type='string' # need an id to send to Oracle file_id=db.wiki_media.insert(filename=filename) db.commit() if file_id: print 'file_id=' + str(file_id) db.executesql('UPDATE WIKI_MEDIA SET FILEDATA =:blobdoc WHERE ID =:file_id', placeholders={'file_id':file_id, 'blobdoc':blobdoc}) db.commit() stream.close() return file_idYes. That is what I meant. I even chased down the call in the code to see where you got it.
# myfile is an open file object
file_id=db.mytable.insert(myupload=db.mytable.myupload.store(myfile,
filename="myfilename.txt", path="/home/spametki/"),
myuploadfield=myfile.read())
# -*- coding: cp1252 -*-import os, sysfrom gluon import DAL, Field, fileutilsfrom 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