このコードを使用してファイルをアップロードしようとしています:
<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を使用しています。
また、このページには次のように記載されています: 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 にキー「ファイル」があるかどうかを確認することです