【问题标题】:GAE Python unable to write file properly to Cloud StorageGAE Python 无法将文件正确写入云存储
【发布时间】:2015-05-05 09:18:14
【问题描述】:

在使用 Web 表单(python、webapp2、gae)上传文件并写入云存储 (GCS) 时,我无法弄清楚我做错了什么

jinja2 模板

<form action="/upload_data" method="post">
<input type="file" name="add_scanned_data_file" enctype="multipart/form-data">
<input type="image" src="stylesheets/add_data.png" alt="submit" align="left"></form>

webapp2 代码

fx = self.request.body_file
fx_name = "some_name"
gcs_file = gcs.open(_GCS_BUCKET_NAME + fx_name, 'w')
gcs_file.write(fx.read())
gcs_file.close()

文件被上传到 GCS 但不正确。 GCS 中上传文件的内容仅显示带有一些表单参数字符串的文件名。所以看来我没有从 webform 正确读取输入文件。

_________编辑(下面添加的文字)___________

虽然这个(在表单标签中添加多部分)到目前为止有效并且 (gcs_file.write(fx.read()) 在 GCS 中上传文件。但是当我尝试读取该文件时(从 GCS 浏览器或通过 self.response .write(gcs_file.read()) ),我得到如下输出(这意味着我需要对文件元数据做一些事情??)

                                                  ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_update_date" ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_text" Upload ------WebKitFormBoundaryk9MfD0ETQjUVirBO Content-Disposition: form-data; name="add_scanned_data_file"; filename="sample.jpg" Content-type: image/jpeg ����JFIF,,��xPhotoshop 3.08BIM\gP53616c7465645f5f605b5cccc1a3ffb06bd320d5ad9ccaa25a68c613f04d7ad0dcaed6e1c049c109��C��C

【问题讨论】:

  • 仅供参考,您也可以使用 create_upload_url 函数直接上传到云存储
  • 是的,尽管我现在正在努力理解 gcs.write() 是否实际上首先写入元数据然后写入二进制文件?或者gcs.read()读取元数据+二进制,需要进行一些后处理才能显示为“文件另存为”给最终用户...=
  • gcs 是 python 中类似接口的文件。它把你写的东西写进去,它不知道元数据或二进制文件之类的。
  • 是的。我打开了一个新问题stackoverflow.com/questions/30172782,因为基本上我现在正在努力拆分多格式数据。

标签: python google-app-engine google-cloud-storage webapp2


【解决方案1】:

enctypeform 标签的一个属性:

<form action="/upload_data" method="post" enctype="multipart/form-data">
<input type="file" name="add_scanned_data_file">
<input type="image" src="stylesheets/add_data.png" alt="submit" align="left">
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2021-08-21
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多