开发者

How to write an image to datastore?

UPDATE:

See the comment by systempuntoout. The form was missing the submit button. Now it all works.


I have this code that I think would write an image to datastore; but that's not happenning:

class AvatarSave(webapp.RequestHandler):
    def post(self):
        q = User.all()
        q.filter("userEmail =", "az@example.com")
        qTable = q.fetch(10)
        if qTable:
            for row in qTable:
                avatar = images.resize(self.request.get("img"), 32, 32)
                row.avatar = db.Blob(avatar)
                db.put(qTable)
        else:
            self.response.out.write("user not found")

        self.redirect('/')

The log shows the image key:

INFO     2010-12-04 13:56:26,601 dev_appserver.py:3317] "GET /img?
img_id=ag1oZWxsby0xLXdvcmxkcgsLEgRVc2VyGIABDA HTTP/1.1" 200 -

But nothing is displayed except the broken link. What am I missing here? I would appreciate your help. I include the entire script below. Thanks!

class MainPage(webapp.RequestHandler):
    def get(self):
        siteUser = users.get_current_user()
        greeting = None        
        if siteUser:
            greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
                    (siteUser.nickname(), users.create_logout_url("/")))
        else:
            greeting = ("<a href=\"%s\">Sign in or register</a>" %
                    users.create_login_url("/"))

        self.response.out.write(greeting)            

        self.response.out.write("""
          <form action="/avatar-save" enctype="multipart/form-data" method="post">                
            <div><label>Avatar:</label></div>
            <div><input type="file" name="img"/></div>
          </form>
        </body>
      </html>""")

        query = User.all()
        query.filter("userEmail =", "az@example.com")
        query.order("-userScore")
        results = query.fetch(10)
        self.response.out.write("""<html><head><style>
                               body {font-size: small;
                                     font-family: Verdana, Helvetica, sans-serif;
                                    }</style>
                                    </head><body><ol>""")
        for result in results:
            self.response.out.write("<li>")
            self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
            self.response.out.write("<div><img src='img?img_id=%s'></img>开发者_JS百科" % result.key())
            self.response.out.write("</li>")
        self.response.out.write("</ol></body></html>")

class Image (webapp.RequestHandler):
    def get(self):
        greeting = db.get(self.request.get("img_id"))
        if greeting.avatar:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(greeting.avatar)
        else:
            self.response.out.write("No image")

class AvatarSave(webapp.RequestHandler):
    def post(self):
        q = User.all()
        q.filter("userEmail =", "az@example.com")
        qTable = q.fetch(10)
        if qTable:
            for row in qTable:
                avatar = images.resize(self.request.get("img"), 32, 32)
                row.avatar = db.Blob(avatar)
                db.put(qTable)
        else:
            self.response.out.write("user not found")

        self.redirect('/')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/img', Image),
                                      ('/avatar-save', AvatarSave),                                      
                                      ],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()


I think your code is missing the Submit button on the image form submission:

self.response.out.write("""
          <form action="/avatar-save" enctype="multipart/form-data" method="post">                
            <div><label>Avatar:</label></div>
            <div><input type="file" name="img"/></div>
            <input type="submit"/>
          </form>...

I've tried your code inferring the User model that you have not reported in the question:

class User(db.Model):
    userEmail = db.EmailProperty()
    userScore = db.IntegerProperty()
    avatar = db.BlobProperty()

since your code does not contain the part from creating the user, I have created a user from scratch from the Development interactive console with:

from main import User
User(userEmail='az@example.com', userScore=1).put()

then, I've logged in with az@example.com and I've uploaded the picture.

This is the result:

How to write an image to datastore?

All I can say is "it works on my machine".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜