我正在使用Python在Google App Engine上创建电子商务平台。为此,我需要将我将提供的产品的详细信息存储在一个单独的实体中,其中还包括每个产品的图像。现在,如何在数据存储区中存储图像?另外,更重要的是,如何从数据库中提取图像并在我的应用程序中为用户显示它?
编辑:
将我的图片上传到“图片”实体后,我试图将其显示出来。这是相同的代码:
main.py
class FileUpload(Handler):
def post(self):
file_upload = self.request.POST.get("file", None)
file_name = file_upload.filename
image = Images(id=file_name, file_name=file_name, blob=file_upload.file.read())
image.put()
self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0]
self.response.write(image.blob)
self.final()
def final(self):
images = db.GqlQuery("Select * FROM Images WHERE file_name = 'CN.jpg'")
self.render("template.html", images = images)
class Blob(Handler):
def get(self):
self.render("blob.html")
app = webapp2.WSGIApplication([('/', MainPage),
('/signup', Register),
('/login', Login),
('/logout', Logout),
('/mp', MP),
(r'/file_upload', FileUpload),
('/blob', Blob)], debug=True)
template.html
<html>
<head>
<title>Template</title>
</head>
<body>
{%
for image in images
%}
<div>{{image.blob}}</div>
{%endfor%}
</body>
</html>
blob.html
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Image Upload</title>
</head>
<body>
<form action="/file_upload" enctype="multipart/form-data" method="post">
<div><input type="file" name="file"/></div>
<div><input type="submit" value="Upload"></div>
</form>
</body>
</html>
现在,在template.html中,我期待显示我试图提取的图像。但是,我收到以下错误:
<div>{{image.blob}}</div>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
答案 0 :(得分:6)
上传图像,将其保存在数据存储区并提供图像的基本示例。
fileupload.py:
import webapp2
from google.appengine.ext import ndb
import mimetypes
class Images(ndb.Model):
file_name = ndb.StringProperty()
blob = ndb.BlobProperty()
class FileUpload(webapp2.RequestHandler):
def post(self):
file_upload = self.request.POST.get("file", None)
file_name = file_upload.filename
image = Images(id=file_name, file_name=file_name, blob=file_upload.file.read())
image.put()
self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0]
self.response.write(image.blob)
class ImgServe(webapp2.Requesthandler):
def get(self, resource):
image = ndb.Key('Images', resource).get()
self.response.headers[b'Content-Type'] = mimetypes.guess_type(image.file_name)[0]
self.response.write(image.blob)
以下是发布图片的静态表单:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Image Upload</title>
</head>
<body>
<form action="/file_upload" enctype="multipart/form-data" method="post">
<div><input type="file" name="file"/></div>
<div><input type="submit" value="Upload"></div>
</form>
</body>
</html>
路由表单帖子:
webapp2.Route(r'/file_upload', handler='fileupload.FileUpload')
图片服务器的路由:<img alt="test" src="/img_serve/test.png" />
webapp2.Route(r'/img_serve/<resource:(.*)>', handler='fileupload.ImgServe'))
答案 1 :(得分:2)
您可以使用Google高性能图像服务。您必须将图像存储或上传到blobstore或Google Cloud Storage并创建serve_url。
好处:
更多:在https://developers.google.com/appengine/docs/python/blobstore/投放blob get_serving_url:https://developers.google.com/appengine/docs/python/images/functions
答案 2 :(得分:0)
有几个选项,您可以根据图像大小和工作流程来决定。