代码之家  ›  专栏  ›  技术社区  ›  Phaedrus

在appengine(python)中处理文件上传

  •  0
  • Phaedrus  · 技术社区  · 15 年前

    我正在尝试使用appengine和python实现一个文件上传解决方案。我正在努力检查是否有一个文件附在表格上。我正在设置enctype=“multipart/form data”,原则上它可以工作。我的python处理程序如下所示:

    fileupload = self.request.POST["content"]
    
    if not fileupload:
        return self.error(400)
    

    如果没有附加文件,则此操作有效。但是,如果附加了文件,则会出现以下错误:

      File "D:\eclipse_dev\workspace\test\src\handlers.py", line 351, in post
        if not fileupload:
      File "C:\Python25\lib\cgi.py", line 633, in __len__
        return len(self.keys())
      File "C:\Python25\lib\cgi.py", line 609, in keys
        raise TypeError, "not indexable"
    TypeError: not indexable
    

    在处理程序中执行其他操作之前,如何安全地检查是否存在上载?

    谢谢你的帮助。

    当做, 罗宾

    4 回复  |  直到 15 年前
        1
  •  1
  •   Adam Crossland    15 年前

    怎么样:

    fileupload = self.request.POST["content"]
    
    if fileupload is None:
        return self.error(400)
    
        2
  •  0
  •   Phaedrus    15 年前

    import cgi
    .
    .
    fileupload = self.request.POST["content"]
    
    if not isinstance(fileupload, cgi.FieldStorage):
       return self.error(400)
    

    不确定这是否是最好的解决方案,但似乎是可行的。

        3
  •  0
  •   Worldcrafter DavidGSola    13 年前

    "The request object provides a get() method that returns values for arguments parsed from the query and from POST data."

    请尝试以下操作:

    def post(self):
      fileupload = self.request.get('content')
      if not fileupload:
        self.error(400)
      else:
        # Proceed normally
    
        4
  •  0
  •   specialscope    13 年前

    我有一个关于文件上传到GAE的小教程。 http://verysimplescripts.blogspot.jp/