Web2py:我应该如何显示存储在数据库中的上传图像?
有没有一种 web2py 方式来显示数据库表中的图像?
示例:
模型:
db.define_table=('images',Field('picture', 'upload' ))
控制器:
def somefunction(): to get the image.
我到底应该如何从数据库“读取”图片?
观点:
<img src="{{somefunction}}" />
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}}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,您的模型不会将图像存储在数据库中,而是将图像存储在文件系统上,并将其新文件名存储在数据库中(在“图片”字段中)。如果要将图像本身存储在数据库中,请使用以下命令:
无论将图像存储在文件系统上还是数据库中,都可以使用相同的方法来检索它们。 “welcome”脚手架应用程序在
default.py
控制器中包含以下download()
操作:要检索图像,只需执行以下操作:
where
picture_name< /code> 是存储在您要检索的特定图像的“
images
”表的“picture
”字段中的值。有关更多详细信息,请参阅此处和此处。
如果您需要进一步帮助,请尝试在邮件列表上提问。
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:
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:To retrieve an image, just do something like:
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 将图像作为文件上传的默认方式,您可以使用:
在模型中:
在控制器中: 在
default/somefunction.html 视图中:
我知道这是在原始问题之后一段时间,但认为它可能有用因为我花了一段时间才弄清楚。
Alternatively, if you use web2py's default way of uploading images as files, you can use:
In models:
In controllers:
And in the default/somefunction.html view:
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.