我写了一些代码。
我可以在 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)