1

我写了一些代码。

我可以在 BobProperty 中上传图片。

但我无法将图像加载到 HTML 页面中

import webapp2
from google.appengine.ext import db
from google.appengine.api import images

form="""
<form action="/" enctype="multipart/form-data" method="post">
    <div><label>Avatar:</label></div>
        <div><input type="file" name="img"/></div>
            <input type="submit" value="switch">
</form>
"""

创建 BlobProperty

class Greeting(db.Model):
    avatar = db.BlobProperty() 

图像

class Image(webapp2.RequestHandler):
    def get(self):
        posts = db.GqlQuery("select * from Greeting")
        for post in posts:

            self.response.headers['Content-Type'] = 'image/jpg'
            self.response.out.write(post)

用于将图像放入数据存储区

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)
    def post(self):
        avatar = images.resize(self.request.get('img'), 32, 32)
        greeting = Greeting(avatar=avatar)
        greeting.avatar = db.Blob(avatar)
        greeting.put()    


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/img', Image),],
                               debug=True)
4

1 回答 1

0

您应该进行以下更改:

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)
    def post(self):
        avatar = images.resize(self.request.get('img'), 32, 32)
        greeting = Greeting()
        greeting.avatar = db.Blob(avatar)
        greeting.put()

此外,在提供图像的处理程序中:

class Image(webapp2.RequestHandler):
    def get(self):
        posts = db.GqlQuery("select * from Greeting")
        for post in posts:

            self.response.headers['Content-Type'] = 'image/jpg'
            self.response.out.write(post.avatar)
于 2012-11-11T19:26:24.607 回答