我正在使用以下代码:
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
我做错了什么?
谢谢!
PS - 我知道这不是最健壮的代码,但它仅用于我的内部项目,它不是按比例构建的。
最佳答案
我最终设法修复了它,我认为问题出在 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())
关于python - Appengine 上的 file.read() 返回垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4816601/