2012-05-23 224 views
2

我正在閱讀有關Google App Engine中的Blobstore的信息。下面的代碼來自示例文檔。在用戶選擇要上傳的文件並單擊提交後,如何將密鑰轉換爲JavaScript變量?我可以在一個頁面上顯示它,但我只想保留它以備後用。很明顯,我是網絡編程新手。獲取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.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() 

回答

6

你可以做這樣的事情:

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

感謝戴夫。然後,我修改了Dave的代碼以使用模板。我在使用模板引用外部.js文件時遇到了問題,直到我意識到它們必須位於static_dir中。 – RFF