3
我正在嘗試學習blobstore API ...並且我能夠成功上傳文件並將它們還原回來,但我沒有嘗試將其合併一個帶有常規web窗體的上傳表單,能夠與文件關聯額外信息,例如文件的暱稱。需要使用Google App Engine處理上傳表單的幫助Blobstore
以下是我一直在玩的簡單應用程序的代碼。它基於谷歌提供的示例。
#!/usr/bin/env python
#
import os
import urllib
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
class StoredFiles(db.Model):
nickname = db.StringProperty()
blobkey = blobstore.BlobReferenceProperty()
@staticmethod
def get_all():
query = db.Query(StoredFiles)
files = query.get()
return files
def doRender(handler, page, templatevalues=None):
path = os.path.join(os.path.dirname(__file__), page)
handler.response.out.write(template.render(path, templatevalues))
class MainHandler(webapp.RequestHandler):
def get(self):
allfiles = StoredFiles.get_all()
upload_url = blobstore.create_upload_url('/upload')
templatevalues = {
'allfiles': allfiles,
'upload_url': upload_url,
}
doRender(self, 'index.html', templatevalues)
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
self.redirect('/save/%s' % blob_info.key())
class SaveHandler(webapp.RequestHandler):
def get(self, resource):
newFile = StoredFiles()
newFile.nickname = self.request.get('nickname')
resource = str(urllib.unquote(resource))
newFile.blobkey = resource
newFile.put()
self.redirect('/')
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
def main():
application = webapp.WSGIApplication(
[('/', MainHandler),
('/upload', UploadHandler),
('/save/([^/]+)?', SaveHandler),
], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()
根據該文檔,Blob存儲區進行處理程序應該通過Blob鍵和表格的其餘部分其重定向到... Blob鍵通就好傳來的處理程序,但沒有別的是。
有人可以指出我搞亂了什麼或指向我描述這個用例的好教程嗎?
謝謝!我以爲我之前曾嘗試過這樣的事情,但都失敗了,但我確信還有一些其他錯誤正在蔓延。今天晚些時候將嘗試您的解決方案。 – Sologoub 2010-10-08 15:39:00
完美無缺!謝謝! – Sologoub 2010-10-09 04:38:25
Np,很高興它的工作。 :) – Franck 2010-10-10 00:58:46