Я пытаюсь загрузить файл, используя этот код:
<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:
и вместо этого проверить, есть ли в self.request.POST ключ 'file' с помощью метода python has_key