Hi I have some images stored as BlobProperty in Google Cloud Datastore. I am trying to load these images via ajax into my template. For eg:- a user has an image and a name. Now the image and name area gets populated via an AJAX get call to the server. I am not understanding how to I send these images to the client , JSON wont support binary data. However googling around tells me of something called base 64.(I am quite new to all this, so let me admit , I am a noob).
您好我在Google Cloud Datastore中将一些图像存储为BlobProperty。我试图通过ajax将这些图像加载到我的模板中。例如: - 用户有图像和名称。现在,通过对服务器的AJAX get调用来填充图像和名称区域。我不明白如何将这些图像发送给客户端,JSON不支持二进制数据。然而谷歌搜索告诉我一个叫做base 64的东西。(我对这一切都很新,所以让我承认,我是一个菜鸟)。
Is this the only way to handle this or is there some other better way.
这是处理这个的唯一方法还是有其他更好的方法。
2 个解决方案
#1
1
This thread suggests that if you just create an image element, set its src, and add it to your page using Javascript, the browser will take care of making an HTTP request for the image:
这个主题建议如果你只是创建一个图像元素,设置它的src,并使用Javascript将它添加到你的页面,浏览器将负责为图像发出HTTP请求:
http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images
http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images
If you do want to do it with 'pure' AJAX, then base64 is probably the best thing: it's a way of encoding binary data (like images) as text, so you can send it as a long string in json.
如果你想用'纯'AJAX做,那么base64可能是最好的东西:它是一种将二进制数据(如图像)编码为文本的方式,因此你可以将它作为长字符串发送到json中。
#2
0
This is how I do it, it's in flask but nonetheless it's python this way, you create a request handler to display the images.
这就是我的方式,它是在烧瓶中,但尽管如此它是python这样,你创建一个请求处理程序来显示图像。
So all you need to do to get the image via ajax is getting the image id to be served. It's simpler and you can manipulate the size as well on the fly
因此,通过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
this is what I do to manipulate the size
这就是我操纵尺寸的方法
@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
hope that helps
希望有所帮助
#1
1
This thread suggests that if you just create an image element, set its src, and add it to your page using Javascript, the browser will take care of making an HTTP request for the image:
这个主题建议如果你只是创建一个图像元素,设置它的src,并使用Javascript将它添加到你的页面,浏览器将负责为图像发出HTTP请求:
http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images
http://bytes.com/topic/javascript/answers/472046-using-ajax-xmlhttprequest-load-images
If you do want to do it with 'pure' AJAX, then base64 is probably the best thing: it's a way of encoding binary data (like images) as text, so you can send it as a long string in json.
如果你想用'纯'AJAX做,那么base64可能是最好的东西:它是一种将二进制数据(如图像)编码为文本的方式,因此你可以将它作为长字符串发送到json中。
#2
0
This is how I do it, it's in flask but nonetheless it's python this way, you create a request handler to display the images.
这就是我的方式,它是在烧瓶中,但尽管如此它是python这样,你创建一个请求处理程序来显示图像。
So all you need to do to get the image via ajax is getting the image id to be served. It's simpler and you can manipulate the size as well on the fly
因此,通过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
this is what I do to manipulate the size
这就是我操纵尺寸的方法
@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
hope that helps
希望有所帮助