您好我有一些图像存储为 Google Cloud Datastore 中的 BlobProperty。我正在尝试通过 ajax 将这些图像加载到我的模板中。例如:- 用户有图像和名称。现在图像和名称区域通过对服务器的 AJAX get 调用填充。我不明白如何将这些图像发送到客户端,JSON 不支持二进制数据。然而,谷歌搜索告诉我一个叫做 base 64 的东西。(我对这一切都很陌生,所以让我承认,我是一个菜鸟)。
这是处理此问题的唯一方法还是有其他更好的方法。
您好我有一些图像存储为 Google Cloud Datastore 中的 BlobProperty。我正在尝试通过 ajax 将这些图像加载到我的模板中。例如:- 用户有图像和名称。现在图像和名称区域通过对服务器的 AJAX get 调用填充。我不明白如何将这些图像发送到客户端,JSON 不支持二进制数据。然而,谷歌搜索告诉我一个叫做 base 64 的东西。(我对这一切都很陌生,所以让我承认,我是一个菜鸟)。
这是处理此问题的唯一方法还是有其他更好的方法。
该线程建议,如果您只是创建一个图像元素,设置其 src,并使用 Javascript 将其添加到您的页面,浏览器将负责为图像发出 HTTP 请求:
http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images
如果您确实想使用“纯”AJAX 来实现,那么 base64 可能是最好的选择:它是将二进制数据(如图像)编码为文本的一种方式,因此您可以将其作为 json 中的长字符串发送。
这就是我的做法,它在烧瓶中,但它仍然是 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
希望有帮助