0

我正在尝试在不使用表单的情况下将文件上传到我的 Google App 中的 blobstore。但我被困在如何让应用程序读取我的本地文件。我对 python 和 Google Apps 还很陌生,但是经过一些剪切和粘贴后,我得到了这样的结果:

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)

现在的问题是我真的不知道如何获取本地文件

4

1 回答 1

2

您无法从应用程序本身内部读取本地文件系统,您需要使用 http POST 将文件发送到应用程序。

您当然可以在另一个应用程序中执行此操作 - 您只需要使用文件内容创建 mime 多部分消息并将其发布到您的应用程序,发送应用程序只需创建您将手动发布到应用程序的 http 请求。您应该阅读有关如何使用 c# 创建 mime mulitpart 消息的内容。

于 2012-06-18T01:17:00.937 回答