Select image path and content from a db(table).select()

21 views
Skip to first unread message

Luciano Laporta Podazza

unread,
Aug 8, 2017, 6:28:12 PM8/8/17
to web2py-users
Hello,

I was wondering how to do a web service (json) to select all rows from a table, where there's an 'upload' field type and return them in JSON, with "upload" field data (images) encoded in base64. 
The problem I'm having is that when I do the db(table_name).select() I'm only getting the image path but not the conten. It would be nice to get that in a few lines of code instead of having to build my own logic.

Hope it makes sense to you.

Thanks in advance!

Leonel Câmara

unread,
Aug 8, 2017, 8:30:01 PM8/8/17
to web2py-users
I haven't tested it, but you could do something like 

import base64


def my_awesome_api():
   
def b64representer(path, row):
        fname
, stream = db.table_name.your_upload_field.retrieve(path)
       
return {'filename': fname, 'data': base64.b64encode(stream.read())}
       
    db
.table_name.your_upload_field.represent = b64representer
   
return response.json(list(db(db.table_name.id > 0).select().render()))

Reply all
Reply to author
Forward
0 new messages