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

本文是小编为大家收集整理的关于Web2py。我应该如何显示一个存储在数据库中的上传图片?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有Web2PY从数据库表显示图像的方法?

示例:

模型:

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

控制器:

def somefunction(): to get the image.

我应该如何从数据库中"读取"图片?

视图:

<img src="{{somefunction}}" />

推荐答案

,您的模型不会将图像存储在数据库中 - 相反,它将将图像存储在文件系统上,并将其新文件名存储在数据库中(在"图片"字段中).如果要将图像本身存储在数据库中,请使用以下内容:

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

无论是将图像存储在文件系统还是在数据库中,都可以使用相同的方法来检索它们. "欢迎"脚手架应用程序包括default.py控制器中的以下download()操作:

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

要检索图像,只需做以下操作:

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

其中picture_name是要检索的特定图像的'images'表中存储的值.

有关更多详细信息,请参见在这里 =" http://web2py.com/book/default/chapter/11#upload-files-in-database" rel =" nofollow">在这里.

如果您需要进一步的帮助,请尝试询问邮件列表.<./p>

其他推荐答案

另外,如果您使用Web2Py上传图像作为文件的默认方式,则可以使用:

在模型中:

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

在控制器中:

def somefunction():
    pic = db(db.images).select().first().picture   #select first picture
    return dict(pic=pic)

和默认/somefunction.html视图:

{{extend 'layout.html'}}
<img  src="{{=URL( 'download', args=pic)}}" />

我知道这是在原始问题之后的一段时间,但认为这可能很有用,因为我花了一段时间才弄清楚.

本文地址:https://www.itbaoku.cn/post/597560.html

问题描述

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.

其他推荐答案

Alternatively, if you use web2py's default way of uploading images as files, you can use:

In models:

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

In controllers:

def somefunction():
    pic = db(db.images).select().first().picture   #select first picture
    return dict(pic=pic)

And in the default/somefunction.html view:

{{extend 'layout.html'}}
<img  src="{{=URL( 'download', args=pic)}}" />

I know this is a while after the original question but thought it might be useful as it took me a while to figure out.