繁体   English   中英

使用Python Google App Engine API将文件上传到Google存储空间

[英]upload file to google storage using Python Google App engine API

我正在关注Massimiliano Pippi的适用于Google App引擎的Python 在第3章中,我们尝试上传一个文件,我的应用程序的用户通过此html代码选择了该文件:

<div class="form-group">
            <label for="uploaded_file">Attached file:</label>
            <input type="file" id="uploaded_file" name="uploaded_file">
</div>

从python部分的MainHandler的MainHandler中,我使用以下命令获取请求内容:

def post(self):
    uploaded_file = self.request.POST.get("uploaded_file", None)
    file_name = getattr(uploaded_file, 'filename')
    file_content = getattr(uploaded_file, 'file', None)
    content_t = mimetypes.guess_type(file_name)[0]

    bucket_name = app_identity.get_default_gcs_bucket_name()
    path = os.path.join('/', bucket_name, file_name)
    with cloudstorage.open(path, 'w', content_type=content_t) as f:
        f.write(file_content.read())

问题是,该变量uploaded_file ,如果它是通过皮皮的Massimiliano文件的处理,但我的Python告诉我,这个变量是包含该文件的名称Unicode。 因此,当我尝试file_name = getattr(uploaded_file, 'filename') ,出现错误。

显然,书中的代码是错误的,我该如何解决?

好了,在蒂姆的建议下,我选择了WebOb的文档,并注意到在html文件中,我应该将enctype="multipart/form-data"放置如下:

<form action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="uploaded_file">Attached file:</label>
        <input type="file" id="uploaded_file" name="uploaded_file">
    </div>
</form>

代替:

<form action="" method="post">
    <div class="form-group">
        <label for="uploaded_file">Attached file:</label>
        <input type="file" id="uploaded_file" name="uploaded_file">
    </div>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM