Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web2py: How should I display an uploaded image that is stored in a database?

Is there a web2py way of displaying images from a database table?

Example:

The model:

db.define_table=('images',Field('picture', 'upload' ))

The controller:

def somefunction(): to get the image.

How exactly should I "read" a picture from the database?

The view:

<img src="{{somefunction}}" />
like image 967
Florin Avatar asked Jun 13 '11 18:06

Florin


1 Answers

As is, your model will not store the image in the database -- instead, it will store the image on the filesystem, with its new filename stored in the database (in the 'picture' field). If you want to store the image itself in the database, use the following:

db.define_table('images',
    Field('picture', 'upload', uploadfield='picture_file')
    Field('picture_file', 'blob'))

Whether you store the images on the filesystem or in the database, you can use the same method to retrieve them. The 'welcome' scaffolding application includes the following download() action in the default.py controller:

def download():
    return response.download(request, db)

To retrieve an image, just do something like:

<img src="{{=URL('default', 'download', args=picture_name)}}" />

where picture_name is the value stored in the 'picture' field of the 'images' table for the particular image you want to retrieve.

For more details, see here and here.

If you need further help, try asking on the mailing list.

like image 133
Anthony Avatar answered Nov 15 '22 21:11

Anthony