使用Google Cloud Datastore和Ajax(Blobs)-Python
-
09-10-2019 - |
题
嗨,我有一些在Google Cloud DataStore中存储为BlobProperty的图像。我试图通过AJAX加载这些图像进入我的模板。例如: - 用户具有图像和名称。现在,图像和名称区域通过AJAX获取呼叫服务器填充。我不明白如何将这些图像发送给客户,JSON不会支持二进制数据。但是谷歌搜索告诉我一些叫做基础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
希望有帮助
不隶属于 StackOverflow