Google App Engine 中的图像响应
我通过执行以下操作将用户的图像保存为 BlobProperty:
user.image = urlfetch.fetch(image_url).content
然后我使用 url 渲染该图像,例如:
/image/user_id
图像必须保存,因为当我执行 len(user.image) 时,我会得到数千个数字。在本地实例上,图像渲染正常。在已部署的应用程序上,我收到以下错误,当我转到图像网址时,浏览器中没有显示任何内容:
Traceback(最近一次调用最后一次): 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第 86 行,运行中 self.finish_response() 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第 127 行,在 finish_response 中 自写(数据) 文件“/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py”,第202行,写入 断言类型(数据)是 StringType,“write() 参数必须是字符串” AssertionError: write() argument must be string
另外,这是提供图像的处理程序:
class ImageHandler(webapp2.RequestHandler):
""" Returns image based on id. """
def get(self, *args, **kwargs):
user = db.get(
db.Key.from_path('User', models.User.get_key_name(kwargs.get('id'))))
if user.image:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(user.image)
else:
self.response.out.write("No image")
只是为了澄清,我尝试将内容类型设置为 jpeg 和 png。并且在本地服务器上一切正常。任何帮助将不胜感激。谢谢!
I am saving a user's image as a BlobProperty by doing:
user.image = urlfetch.fetch(image_url).content
Then I'm rendering that image using a url such as:
/image/user_id
The image must be saving because because when I do len(user.image) I get a number in the thousands. And on the local instance the image renders ok. On the deployed app, I get the following error, and when I go to the image url nothing shows in the browser:
Traceback (most recent call last):
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py", line 202, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string
Also, here's the handler that serves the image:
class ImageHandler(webapp2.RequestHandler):
""" Returns image based on id. """
def get(self, *args, **kwargs):
user = db.get(
db.Key.from_path('User', models.User.get_key_name(kwargs.get('id'))))
if user.image:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(user.image)
else:
self.response.out.write("No image")
Just to clarify I tried both setting content-type to jpeg and png. And things are working ok on the local server. Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将图像写入 blobstore 然后使用 send_blob() 机制?
http://code.google.com/appengine/docs/python /blobstore/overview.html#Serving_a_Blob
Why not write the image to the blobstore and then use the send_blob() mechanism?
http://code.google.com/appengine/docs/python/blobstore/overview.html#Serving_a_Blob
回答我的问题,这解决了一切:
这很令人困惑,因为文档中的示例没有将 BlobProperty 转换为字符串。
Answering my question, that fixes everything:
It's confusing because the example in the docs does not cast the BlobProperty as string.