我写了一些代码。
我可以在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()
for image
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)
答案 0 :(得分: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)