【问题标题】:Using Google Cloud Datastore and AJAX(blobs)-python使用 Google Cloud Datastore 和 AJAX(blob)-python
【发布时间】:2011-05-09 19:33:37
【问题描述】:

您好,我有一些图像作为 BlobProperty 存储在 Google Cloud Datastore 中。我正在尝试通过 ajax 将这些图像加载到我的模板中。例如:- 用户有图像和名称。现在图像和名称区域通过对服务器的 AJAX get 调用填充。我不明白如何将这些图像发送到客户端,JSON 不支持二进制数据。然而,谷歌搜索告诉我一个叫做 base 64 的东西。(我对这一切都很陌生,所以让我承认,我是一个菜鸟)。

这是处理这个问题的唯一方法还是有其他更好的方法。

【问题讨论】:

    标签: javascript python google-app-engine flask google-cloud-datastore


    【解决方案1】:

    我就是这样做的,它在烧瓶中,但它仍然是 python 这样,您就可以创建一个请求处理程序来显示图像。

    因此,通过 ajax 获取图像所需要做的就是获取要提供的图像 ID。它更简单,您也可以动态调整大小

    from flask import request
    
    from google.appengine.api import taskqueue, images, mail
    from google.appengine.ext import db
    
        @app.route('/image/<img_id>')
        def imgshow(img_id):
          imageuse = Image.all().filter("image_id =", img_id).get()
          if imageuse:
            response = Response(response=imageuse.content)
            #you can use any type over here
            response.headers['Content-Type']='image/png'
            return response
          else:
            return
    

    这就是我用来控制大小的方法

    @app.route('/thumb/<img_id>')
    def thumbshow(img_id):
      imageuse = Image.all().filter("image_id =", img_id).get()
      if imageuse:
        thbimg = images.resize(imageuse.content, 80)
        response = Response(thbimg)
        response.headers['Content-Type']='image/png'
        return response
      else:
        return
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      这个帖子建议如果你只是创建一个图像元素,设置它的 src,并使用 Javascript 将它添加到你的页面,浏览器将负责为图像发出 HTTP 请求:

      http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images

      如果您确实想使用“纯”AJAX 来实现,那么 base64 可能是最好的选择:它是将二进制数据(如图像)编码为文本的一种方式,因此您可以将其作为 json 中的长字符串发送。

      【讨论】:

        猜你喜欢
        • 2020-12-30
        • 2021-06-28
        • 2018-02-14
        • 2019-08-06
        • 2016-12-27
        • 2022-09-30
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多