【发布时间】:2014-02-02 17:25:54
【问题描述】:
我们可以考虑如何在 HTML 中提供图像列表。什么时候我们 提供一个页面,它包含引用图像的标签,然后 浏览器发出单独的请求来获取每个请求。这很好用 与 mongodb。我们可以只提供页面并插入
<img src="/profile_pics/userid">之类的标签,然后我们的服务器只是重定向它 GridFS 文件的路径为{"profile_pic": userid}。
来源:http://www.markus-gattol.name/ws/mongodb.html
我试过了:
HTML
<li>
<img src="/static/img/gridfs/download.jpg">
</li>
瓶装路线
# relevant libraries
import gridfs
from bottle import response
@route('/static/img/gridfs/<filename>')
def server_gridfs_img(filename):
dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
thing = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return thing
错误
https://mysite.com/static/img/gridfs/download.jpg 得到 404 - 尝试直接在浏览器中访问图像 URL 时,以及加载页面时在 Firebug 控制台错误选项卡中。
编辑:
工作代码
@route('/gridfs/img/<filename>')
# OR @route('/gridfs/img/<filename:path>')
def server_gridfs_img(filename):
dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
thing = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return thing
注意:使用static 而不是像gridfs 这样的另一个词会导致上述操作无效。我不知道为什么。测试时所有其他路线都已被注释掉。
【问题讨论】:
标签: mongodb python-2.7 bottle gridfs