Web2py:我应该如何显示存储在数据库中的上传图像? [英] Web2py: How should I display an uploaded image that is stored in a database?
问题描述
是否有从数据库表中显示图片的web2py方法?
示例:
:
db.define_table =('images',Field('picture','upload'))
控制器:
def somefunction():获取图像。
我应该如何从数据库读取图片?
视图:
< img src ={{somefunction}}/>
相反,它将图像存储在文件系统上,其新文件名存储在数据库中(在图片字段中)。如果要将图像本身存储在数据库中,请使用以下命令:
db.define_table('images',
Field('picture','upload',uploadfield ='picture_file')
字段('picture_file','blob'))
无论是将映像存储在文件系统还是数据库中,都可以使用相同的方法来检索它们。 welcome脚手架应用程序在 default.py
控制器中包括以下 download()
操作:
def download():
/ pre>
return response.download(request,db)
要检索图片,只需执行以下操作:
< img src ={{= URL('default','download',args = picture_name)}}/>
其中
picture_name
您要检索的特定图片的图片
表格中的图片
字段。
如果您需要进一步的帮助,请尝试邮寄列表。
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}}" />
解决方案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 thedefault.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.
这篇关于Web2py:我应该如何显示存储在数据库中的上传图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!