我在获取 html 模板中显示的图像时遇到问题。我设法将图像存储到数据存储中。但是,我不知道如何将其发送到 html 文件(或使用 URL 来显示图像?)。请帮助,非常感谢。以下是我的代码:
主文件
# -*- coding: utf-8 -*-
#!/usr/bin/env python2.7
import webapp2
import common
from google.appengine.ext import db
class Image(db.Model):
filename = db.StringProperty(required=True)
data = db.BlobProperty(required=True)
mimetype = db.StringProperty(required=True)
class Test(webapp2.RequestHandler):
def get(self):
common.render(self, 'test.html', {})
def post(self):
imgfile = self.request.POST['image']
img = Image(filename=imgfile.filename, data=imgfile.value, mimetype=imgfile.type)
img.put()
common.render(self, 'test.html', {'imgkey': img.key().id()})
app = webapp2.WSGIApplication([
("/.*", Test)],
debug=True)
测试.html:
<htm>
<head>
<title>Test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<p></p>Upload image: </p>
<form method="post" action="/test" enctype="multipart/form-data">
<input type="file" name="image" /><br />
<input type="submit" name="確定" />
</form>
{% if imgkey %}
Display:<br />
<img src="http://.../disp?key={{imgkey}}" /><br />
{% endif %}
</body>
</html>