appengine中的file.read()返回乱码字符
我正在使用以下代码:
class Image(webapp.RequestHandler):
def get(self, id):
path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id)
self.response.headers['Content-Type'] = 'image/png'
print file(path,'rb').read()
在本地运行时一切正常(也就是说,可以返回图片),但是当我在在线服务器上使用时却得到了乱码。你可以在这里查看输出:http://1.ge0.co/fc/1.png
我哪里做错了呢?
谢谢!
附言 - 我知道这段代码不是最稳健的,但它只是用于我的内部项目,并不是为了大规模使用而设计的。
2 个回答
0
看起来内容类型没有设置正确。
Content-Length:72643
Content-Type:text/html
Date:Thu, 27 Jan 2011 13:01:57 GMT
Server:Google Frontend
这就能解释为什么你看到的是文件的内容,而不是显示出来的图片。
5
我最后终于解决了这个问题,我觉得问题出在 self.response 和 print 的组合上。下面是可以正常工作的代码:
class Image(webapp.RequestHandler):
def get(self, id):
path = os.path.join(os.path.dirname(__file__), 'fcimages/%s.png' % id)
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(file(path, 'rb').read())