2012-05-23 4 views
2

Google App Engine의 Blobstore에 대해 읽었습니다. 아래 코드는 샘플 문서에 있습니다. 사용자가 업로드 할 파일을 선택하고 제출을 클릭하면 키를 자바 스크립트 변수에 어떻게 가져 옵니까? 페이지에 표시 할 수 있지만 나중에 사용하기 위해 보관하고 싶습니다. 분명히, 나는 웹 프로그래밍에 익숙하지 않다.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의 코드를 수정하여 템플릿을 사용했습니다. 나는 static_dir에 있어야한다는 것을 깨달을 때까지 템플릿으로 외부 .js 파일을 참조하는 데 문제가있었습니다. – RFF