簡體   English   中英

從Android在Python AppEngine上上傳並讀取文件

[英]Upload from Android on Python AppEngine and read the file

我想將一個csv文件從Android發送到Python AppEngine。 我正在使用Blobstore API並發送文件,我使用MultipartEntityHttpPostHttpGet

因此,根據Blob存儲API,則必須調用方法create_upload_url('/upload')生成的URL上傳的文件,並使用此URL作為動作要上傳的文件。 如你所見

我在做什么? 我調用一個創建該URL的方法,並將其返回到我的android應用。 並使用此網址上傳文件。

生成的網址采用以下格式:

myapp.appspot.com/_ah/upload/ 大量字母和字母/

基本上是這樣的:

Android代碼

 HttpClient httpClient = new DefaultHttpClient();
 HttpGet httpGet = new HttpGet(mContext.getString("myapp.appspot.com/get_blobstore_url");

 HttpResponse urlResponse = httpClient.execute(httpGet);

 result = EntityUtils.toString(urlResponse.getEntity());

 Uri fileUri = Uri.parse("file:///sdcard/dir/myfile.csv"); // Gets the Uri of the file in the sdcard
 File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri

 FileBody fileBody = new FileBody(file, "multipart/form-data");

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 entity.addPart("file", fileBody);

 HttpPost httpPost = new HttpPost(result);

 httpPost.setEntity(entity);

 HttpResponse response = httpClient.execute(httpPost);
 response.getStatusLine();

AppEngine代碼

# Returns only the URL in which the file will be uploaded, so this URL may be used in client for upload the file
class GetBlobstoreUrl(webapp.RequestHandler):
    def get(self):
        logging.info('here')
        upload_url = blobstore.create_upload_url('/upload')
        logging.info("url blob %s", upload_url)
        self.response.out.write(upload_url)

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        logging.info('inside upload handler')
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        return blob_info.key()

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

if __name__ == '__main__':
    main()

問題1

當我將文件發送到AppEngine返回的網址時,這會自動調用服務器方法UploadHandler嗎? 因為未顯示此方法內的日志消息, 並且正在插入文件 ,並且使用生成的url上傳文件時收到的響應是404錯誤,如果正在上傳文件,為什么會出現該錯誤?

問題2

上傳文件后,如何解析服務器中的csv文件並將文件中的所有數據插入數據存儲區?

謝謝。

當我將文件發送到AppEngine返回的網址時,這會自動調用服務器方法UploadHandler嗎?

正確。

當我將文件發送到AppEngine返回的網址時,這會自動調用服務器方法UploadHandler嗎?

向我們顯示您的服務器日志-您獲得404的網址是什么? 您從上傳處理程序中收到任何日志消息嗎?

上傳文件后,如何解析服務器中的csv文件並將文件中的所有數據插入數據存儲區?

使用BlobReader API ,然后將打開的文件傳遞給python csv模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM