Web2py:我应该如何显示存储在数据库中的上传图像? [英] Web2py: How should I display an uploaded image that is stored in a database?

查看:171
本文介绍了Web2py:我应该如何显示存储在数据库中的上传图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有从数据库表中显示图片的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():
return response.download(request,db)
/ pre>

要检索图片,只需执行以下操作:

 < 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 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.

这篇关于Web2py:我应该如何显示存储在数据库中的上传图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆