【问题标题】:Upload from Android on Python AppEngine and read the file在 Python AppEngine 上从 Android 上传并读取文件
【发布时间】:2011-12-11 21:44:22
【问题描述】:

我想将 csv 文件从 Android 发送到 Python AppEngine。我正在使用 Blobstore API 并发送文件,我使用 MultipartEntityHttpPostHttpGet

因此,根据 Blobstore API,您必须调用方法 create_upload_url('/upload') 来生成上传文件的 url,并将此 url 作为 action 上传文件。如您所见here

我在做什么?我调用了一个方法来创建这个 url 并将其返回到我的 android 应用程序。并使用此网址上传文件。

生成的url格式如下:

myapp.appspot.com/_ah/upload/a-lot-of-numbers-and-letters/

基本上是这样的:

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返回的url时,这会自动调用服务器方法UploadHandler吗?因为没有显示此方法中的日志消息,并且正在插入文件,并且我在使用生成的url上传文件时得到的响应是404错误,为什么如果文件出现错误正在上传吗?

问题 2

我上传文件后,如何解析服务器中的csv文件并将文件中的所有数据插入数据存储区?

谢谢。

【问题讨论】:

  • android 端使用/generate_url,而GAE 端使用/get_blobstore_url。这是代码中的错字,还是只是帖子?
  • 只是帖子。它们是相同的网址。我打错了。修好了。

标签: android python google-app-engine


【解决方案1】:

当我将文件发送到 AppEngine 返回的 url 时,这将 自动调用服务端方法UploadHandler?

正确。

当我将文件发送到 AppEngine 返回的 url 时,这将 自动调用服务端方法UploadHandler?

向我们展示您的服务器日志 - 您获取 404 的 URL 是什么?您是否从上传处理程序收到任何日志消息?

上传文件后,如何解析服务器中的csv文件 并将文件中的所有数据插入数据存储区?

使用BlobReader API,并将打开的文件传递给pythoncsv模块。

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    相关资源
    最近更新 更多