appengine上的file.read()返回垃圾字符

时间:2011-01-27 12:59:19

标签: python google-app-engine file-io

我正在使用以下代码:

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 - 我知道这不是最强大的代码,但它只用于我的内部项目,它不是按比例构建的。

2 个答案:

答案 0 :(得分: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())

答案 1 :(得分:0)

看起来Content-Type设置不正确

Content-Length:72643
Content-Type:text/html
Date:Thu, 27 Jan 2011 13:01:57 GMT
Server:Google Frontend

这可以解释为什么你会看到文件的内容而不是渲染的图像。

相关问题