2012-08-07 4 views
3

제공된 코드 here (주로 GAE 문서에 기반한 설명 중 하나)을 사용하여 이미지를 내 Google App Engine Blobstore에 업로드 할 수있었습니다. 여기 GAE - 모바일 장치에서 Blobstore로 이미지 업로드

는 참조 용으로 전체 코드이다

def get(self): 
    upload_url = blobstore.create_upload_url('/upload') 

그러나

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 

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>""") 

     for b in blobstore.BlobInfo.all(): 
      self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>') 

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
     upload_files = self.get_uploads('file') 
     blob_info = upload_files[0] 
     self.redirect('/') 

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
    def get(self, blob_key): 
     blob_key = str(urllib.unquote(blob_key)) 
     if not blobstore.get(blob_key): 
      self.error(404) 
     else: 
      self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True) 

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

if __name__ == '__main__': 
    main() 

,이 코드는 업로드 URL은 이미지 데이터를 포함 POST 요청 전에 GET 요청을 생성 할 수 있어야 모바일 장치에서 이미지를 보내려고하면 서버 코드를 def post(self): 함수 아래의 단일 코드 블록으로 푸시하고이 작업을 수행하는 데 문제가 있습니다.

위의 행을 def post(self): 코드로 옮기는 것은 트릭을 수행하지 않는 것 같습니다.

아이디어가 있으십니까?

건배! 브렛

답변

0

이것은 기본 Blobstore 업로드 처리기에서 작동하지 않습니다. 첫 번째 다운로드 URL을 생성하고 두 번째 요청은 실제로이 URL에 대한 두 가지 요청이 필요합니다.

모두를 원한다면 직접 file upload handler을 만들고 새로운 Blobstore API를 write files으로 사용하십시오.

2

예 urlfetch를 사용하면 하나의 코드 블록으로 만들 수 있습니다. 당신은 항상 SomeHandler에 파일을 업로드하기보다는 먼저만을 업로드 업로드 URL을 얻을 수 있습니다

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
     upload_files = self.get_uploads('file') 
     if len(upload_files) > 0 : 
      blob_info = upload_files[0] 
      self.response.write(str(blob_info.key())) 
     else: 
      self.error(404) 

class SomeHandler(webapp.RequestHandler): 
    def post(self): 
     file = self.request.POST.get('file') 
     if (file is not None): 
      # Use urlfetch to call to the blob upload url and get result 
      # Just copy the same request body and header and pass to UploadHandler here 
      result = urlfetch.fetch(
       url= blobstore.create_upload_url('/upload'), 
       payload=self.request.body, 
       method=urlfetch.POST, 
       headers=self.request.headers) 
      if result.status_code == 200: 
       blob_key_str = result.content 
       # Get blob key 
       blob_key = blobstore.BlobKey(blob_key_str) 
       # Maybe a url for the file 
       blob_url = images.get_serving_url(blob_key_str, 400) 

이 방법 : 여기 내 접근 방식입니다.

희망 하시겠습니까?

+0

안녕하세요. 이 솔루션은 localhost에서는 제대로 작동하지만 Google 서버에 업로드하면 제대로 작동하지 않습니다. 어떤 아이디어? – Tejas

+0

제 경우에는 작동합니다. 로그를 확인해야합니다. 다른 문제가있을 수 있습니다. –

관련 문제