如何在 <img> 中链接到 GridFS 中的图像标签? [英] How to link to an image in GridFS in an <img> tag?

查看:49
本文介绍了如何在 <img> 中链接到 GridFS 中的图像标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

我们可以考虑在 HTML 中提供图像列表是如何工作的.什么时候我们提供一个页面,它包含引用图像的标签,然后浏览器发出一个单独的请求来获取每个请求.这很好用与 mongodb.我们可以只为页面提供服务并插入像 <img 这样的标签src="/profile_pics/userid"> 然后我们的服务器只是重定向带有 {"profile_pic": userid} 的 GridFS 文件的路径.

来源:http://www.markus-gattol.name/ws/mongodb.html

我试过了:

HTML

  • <img src="/static/img/gridfs/download.jpg">
  • 瓶子路线

    # 相关库导入网格从瓶子进口反应@route('/static/img/gridfs/<文件名>')def server_gridfs_img(文件名):dbname = 'grid_files'db = 连接 [dbname]fs = gridfs.GridFS(db)thing = fs.get_last_version(文件名=文件名)response.content_type = '图像/jpeg'退货

    错误

    我为 https://mysite.com/static/img/gridfs/download.jpg 得到 404 - 在尝试直接在浏览器中和在 Firebug 中访问图像 URL 时加载页面时控制台错误选项卡.

    工作代码

    @route('/gridfs/img/')# OR @route('/gridfs/img/')def server_gridfs_img(文件名):dbname = 'grid_files'db = 连接 [dbname]fs = gridfs.GridFS(db)thing = fs.get_last_version(文件名=文件名)response.content_type = '图像/jpeg'退货

    注意:使用 static 而不是像 gridfs 这样的另一个词会导致上述方法不起作用.我不知道为什么.所有其他路由在测试时都被注释掉了.

    解决方案

    这对我有用.首先在 Python shell 中:

    <预><代码>>>>导入 pymongo、gridfs>>>db = pymongo.MongoClient().my_gridfs>>># 读取我放在当前目录中的文件.>>>img_contents = open('image.jpg', 'rb').read()>>>gridfs.GridFS(db).put(img_contents, filename='image.jpg')ObjectId('52d6c182ca1ce955a9d60f57')

    然后在 app.py 中:

    导入 gridfs进口pymongo从瓶子导入路径、运行、模板、响应连接 = pymongo.MongoClient()@route('/static/img/gridfs/<文件名>')def gridfs_img(文件名):dbname = 'my_gridfs'db = 连接 [dbname]fs = gridfs.GridFS(db)thing = fs.get_last_version(文件名=文件名)response.content_type = '图像/jpeg'退货运行(主机=本地主机",端口=8080)

    当我访问 http://localhost:8080/static/img/gridfs/image.jpg 时,我在浏览器中看到了我的图片.

    我注意到您在示例 URL 中输入了https://",您使用的是 Bottle 插件还是像 Apache 这样的前端服务器?

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

    Source: http://www.markus-gattol.name/ws/mongodb.html

    I have tried:

    HTML

    <li>
    <img src="/static/img/gridfs/download.jpg">
    </li>
    

    Bottle Route

    # 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
    

    Error

    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.

    Edit:

    Working Code

    @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
    

    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.

    解决方案

    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')
    

    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)
    

    When I visit http://localhost:8080/static/img/gridfs/image.jpg I see my image in the browser.

    I notice you put "https://" in the example URL, are you using a Bottle plugin or a frontend server like Apache?

    这篇关于如何在 &amp;lt;img&gt; 中链接到 GridFS 中的图像标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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