<form action="${h.url.current()}" method="POST" enctype="multipart/form-data">
<input type="file" name="your_file" />
</form>
class MyValidator(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
name = formencode.validators.String(not_empty=True, messages={'empty': 'Please enter your name'})
your_file = formencode.validators.FieldStorageUploadConverter(not_empty=True, messages={'empty': 'You haven\'t selected any files'})
try:
form_result = MyValidator().to_python(dict(request.params))
except formencode.Invalid as error:
return 'failed'
else:
print type(form_result['your_file'])
那是我的代码。form_result['your_file']
是一个 Unicode 对象。因此,我无法读取文件或执行任何与“文件”相关的任务。我发现访问它的唯一方法是在执行验证后放弃 formcode 并恢复为request.POST['your_file']
.
我究竟做错了什么?