如何在< img>中链接到GridFS中的图像。标签? [英] How to link to an image in GridFS in an <img> tag?
问题描述
我们可以考虑如何在HTML中投放图片列表。当我们
服务一个页面时,它包含引用图像的标签,然后
浏览器会发出一个单独的请求来获取每个图像。
在mongodb中效果很好。我们可以只提供页面并插入< img
之类的标签,然后我们的服务器就重定向该
src = / profile_pics / userid>
使用{ profile_pic:userid}
到GridFS文件的路径。
We can think about how serving a list of images works in HTML. When we serve a page it includes tags that reference an image and then the browser makes a separate request to get each one. This works well with mongodb. We can just serve the page and insert tags like
<img src="/profile_pics/userid">
and then our server just redirects that path to the GridFS file with{"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
错误
我得到的404 https://mysite.com/static /img/gridfs/download.jpg
-尝试直接在浏览器中访问图像URL时,以及加载页面时在Firebug控制台错误标签中。
I get a 404 for https://mysite.com/static/img/gridfs/download.jpg
- both when trying to access the image URL directly in a browser, and in the Firebug console error tab when loading the page.
编辑:
工作代码
@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
之类的其他词以上不起作用。我不知道为什么测试时已注释掉所有其他路线。
Note: Using static
instead of another word like gridfs
caused the above not to work. I don't know why. All other routes had been commented out when testing.
推荐答案
这对我有用。首先在Python shell中:
this works for me. First in the Python shell:
>>> import pymongo, gridfs
>>> db = pymongo.MongoClient().my_gridfs
>>> # Read a file I have put in the current directory.
>>> img_contents = open('image.jpg', 'rb').read()
>>> gridfs.GridFS(db).put(img_contents, filename='image.jpg')
ObjectId('52d6c182ca1ce955a9d60f57')
然后在app.py中:
Then in app.py:
import gridfs
import pymongo
from bottle import route, run, template, response
connection = pymongo.MongoClient()
@route('/static/img/gridfs/<filename>')
def gridfs_img(filename):
dbname = 'my_gridfs'
db = connection[dbname]
fs = gridfs.GridFS(db)
thing = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return thing
run(host='localhost', port=8080)
当我访问 http:// localhost:8080 / static / img / gridfs / image.jpg
我在浏览器中看到了我的图像。
When I visit http://localhost:8080/static/img/gridfs/image.jpg
I see my image in the browser.
我注意到您在示例URL中输入了 https://,是否使用了Bottle插件还是像Apache这样的前端服务器?
I notice you put "https://" in the example URL, are you using a Bottle plugin or a frontend server like Apache?
这篇关于如何在< img>中链接到GridFS中的图像。标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!