我正在使用Python在Google App Engine上創建電子商務平臺。爲此,我需要將我將提供的產品的細節存儲在一個單獨的實體中,該實體還將包含每個單獨產品的圖像。現在,我如何將圖像存儲在數據存儲中?另外,更重要的是,如何從數據庫中提取圖像並在應用程序中將其顯示給用戶?Google App Engine:將圖像存儲在數據庫(Python)中
編輯:
上傳我的形象到「圖片」實體後,我試圖顯示它。下面是相同的代碼:
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)
看看blobstore。另請參閱圖像文檔。有可能是有幫助的:https://developers.google.com/appengine/docs/python/images/usingimages – mgilson