【问题标题】:How do I Pass Html form image arrays to the Google App Engine(Python)?如何将 Html 表单图像数组传递给 Google App Engine(Python)?
【发布时间】:2011-08-02 04:30:48
【问题描述】:

我正在使用 Google App Engine 的 Python 实现。当我尝试从 html 表单传递图像输入标签数组时,我似乎无法在 Python 控制器中获取它。

HTML:

<form action="/addImages" method="post" enctype="multipart/form-data">
  <input type="file" name="images" />
  <input type="file" name="images" />
  <input type="file" name="images" />
</form>

Python:

class AddImages(webapp.RequestHandler):
def post(self):
    images = self.request.get_all('images')

    for img in images:
        if img != None:
            img_entity.blob = db.Blob(img)

    self.response.out.write("Upload Succeeded")

当我尝试这个时,我得到了可怕的 unicode/str 错误:

TypeError: Blob() argument should be str instance, not unicode

在哪里:

db.Blob(img)

我遵循了大部分教程,但似乎都没有讨论这个特定问题。

【问题讨论】:

  • 你试过img.encode()db.Blob()吗?
  • 我可以将 unicode 编码为 utf-8 或 ascii,但这不会丢弃一些文件数据吗?
  • 您知道,如果您尝试在所有字段中上传超过 10MB 的数据,您将会遇到过大的请求,对吧?我建议使用 BlobStore。它让很多这种头痛消失了。

标签: python html google-app-engine forms http-post


【解决方案1】:

你需要包含

enctype="multipart/form-data"

您的表单标签上的属性,请参阅http://www.mail-archive.com/google-appengine@googlegroups.com/msg03314.html。谷歌搜索您的错误会发现其他几个问题实例。

如果您在添加 enctype 后仍有问题,请尝试 db.Blob(str(img)) 和/或 db.Blob(img.encode('utf-8')),正如上述线程所建议的那样。你也可以试试images =self.request.str_GET['images'] 或类似的东西。

【讨论】:

  • agf,如果您阅读我的原始代码,您会看到包含了 enctype。 str(img) 虽然工作得很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-03-04
  • 2011-12-22
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2014-05-18
  • 2015-02-24
  • 2012-01-20
相关资源
最近更新 更多