我正在尝试使用以下代码上传文件:
<form id="form1" action="convert" enctype="multipart/form-data" method="post">
<input type="file" name="file"/>
<div><input id="submit_button" type="submit" value="Upload"/></div>
</form>
class Convert(RequestHandler):
@login_required
def post(self):
session = Session(writer="cookie", session_expire_time = 3600, set_cookie_expires = True)
if session['id']:
file = self.request.POST['file']
if file and file.type and file.value:
但我继续收到此错误:
if file and file.type and file.value:
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"
有趣的是,这曾经工作过!这有什么不对?请注意,我正在使用webapp2。
同样this页面说明:FieldStorage实例可以像Python字典一样编入索引。它允许使用in运算符进行成员资格测试,并且还支持标准字典方法keys()和内置函数len()
完整的堆栈跟踪:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3858, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3792, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 580, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2918, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2822, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2704, in ExecuteOrImportScript
script_module.main()
File "C:\myproject\main.py", line 16, in main
run_wsgi_app(application)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "C:\myproject\webapp2\__init__.py", line 1053, in __call__
return self.wsgi_app(environ, start_response)
File "C:\myproject\webapp2\__init__.py", line 1098, in wsgi_app
self.handle_exception(request, response, e)
File "C:\myproject\webapp2\__init__.py", line 1092, in wsgi_app
self.router.dispatch(self, request, response, match)
File "C:\myproject\webapp2\__init__.py", line 949, in dispatch
handler.handle_exception(e, app.debug)
File "C:\myproject\webapp2\__init__.py", line 942, in dispatch
getattr(handler, method)(*args)
File "C:\myproject\py\decorators.py", line 15, in decorated
return _login_required (self) or func(self, *args, **kwargs)
File "C:\myproject\py\document.py", line 42, in post
if file and file.type and file.value:
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
我修复它的方法是撕掉if file:
,然后使用python has_key方法检查self.request.POST是否有密钥'file'
答案 0 :(得分:0)
我没有在您的代码段或堆栈跟踪中看到足够的数据来判断错误的来源。取而代之的是,这是一个完整的上传示例:
http://blog.notdot.net/2009/9/Handling-file-uploads-in-App-Engine