2012-06-17 3 views
0

양식을 사용하지 않고 Google 애플리케이션의 BLOBstore에 파일을 업로드하려고합니다. 하지만 앱에서 내 로컬 파일을 읽는 방법을 고민하고 있습니다. 나는 파이썬과 Google 애플리케이션에 아주 새로운 해요하지만 일부 잘라 내기 및 붙여 넣기 후 나는이와 결국했습니다Google App (Python 포함)에서 로컬 파일 읽기

import webapp2 
import urllib 
import os 

from google.appengine.api import files 
from poster.encode import multipart_encode 

class Upload(webapp2.RequestHandler): 
    def get(self): 
    # Create the file in blobstore 
    file_name = files.blobstore.create(mime_type='application/octet-stream') 

    # Get the local file path from an url param 
    file_path = self.request.get('file') 

    # Read the file 
    file = open(file_path, "rb") 
    datagen, headers = multipart_encode({"file": file}) 
    data = str().join(datagen) # this is supposedly memory intense and slow 

    # Open the blobstore file and write to it 
    with files.open(file_name, 'a') as f: 
     f.write(data) 

    # Finalize the file. Do this before attempting to read it. 
    files.finalize(file_name) 

    # Get the file's blob key 
    blob_key = files.blobstore.get_blob_key(file_name) 

문제는 지금은 정말 로컬 파일

잡아하는 방법을 몰라
+0

양식이없는 이유는 무엇입니까? 어떻게 사용자가 파일을 선택하고 파일 입력없이 업로드 할 것으로 예상합니까? –

+0

파일을 저장 한 다음 업로드해야하는 독립 실행 형 프로그램을 만들고 있습니다. – Marb

+0

로컬 파일이 파이썬 코드로 업로드 한 파일을 제출하려고하거나 파일을 제출하려고한다고 말하면 로컬 컴퓨터에 있습니까? GAE가 로컬 파일을 보안 프로토콜로 액세스 할 수 없기 때문에 전자가 가능하지 않습니다. 전적으로 나는 당신이 업로드 필드와 웹 페이지 양식이 필요하다고 말할 것입니다. – gabeio

답변

2

응용 프로그램 자체에서 로컬 파일 시스템을 읽을 수 없으면 http POST를 사용하여 파일을 응용 프로그램에 보내야합니다.

확실히 다른 애플리케이션에서이 작업을 수행 할 수 있습니다. 파일 내용으로 MIME 다중 파트 메시지를 작성하고 애플리케이션에 게시하기 만하면 전송 애플리케이션에서 게시 할 http 요청을 만들어야합니다. 앱을 수동으로 C#을 사용하여 MIME 다중 물 메시지를 작성하는 방법에 대한 정보가 있어야합니다.

+0

고마워요. 내가 잘못 끝까지 쳐다 보지 않고 있다는 걸 깨달았습니다. – Marb

관련 문제