【问题标题】:Getting a Blobstore key获取 Blobstore 密钥
【发布时间】:2012-05-23 20:46:34
【问题描述】:

我正在阅读有关 Google App Engine 中的 Blobstore 的信息。下面的代码来自示例文档。用户选择要上传的文件并单击提交后,如何将密钥放入 javascript 变量中?我可以在页面上显示它,但我只想保留它以供以后使用。显然,我是 Web 编程新手。

#!/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.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
  def get(self):
    upload_url = blobstore.create_upload_url('/upload')
    self.response.out.write('<html><body>')
    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
    name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file')  # 'file' is file upload     field in the form
    blob_info = upload_files[0]
    self.response.out.write('<html><body>')
    self.response.out.write(str(blob_info.key()))
    self.response.out.write('</body><html>')

def main():
  application = webapp.WSGIApplication(
    [('/', MainHandler),
     ('/upload', UploadHandler),
    ], debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

【问题讨论】:

    标签: python google-app-engine blobstore


    【解决方案1】:

    你可以这样做:

    self.response.out.write("""
    <html>
    <script>
    var blobKey = "%s";
    </script>
    <body>
    ...
    </body>
    </html>""" % (blob_info.key(),)
    

    【讨论】:

    • 谢谢戴夫。然后我修改了 Dave 的代码以使用模板。我在使用模板引用外部 .js 文件时遇到了问题,直到我意识到它们必须位于 static_dir 中。
    猜你喜欢
    • 2018-11-29
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2018-02-08
    • 2012-03-14
    相关资源
    最近更新 更多