FormEncode 将我上传的文件作为 Unicode 对象返回 - 如何修复?
<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 对象。因此,我无法读取该文件或执行任何与“文件”相关的任务。我发现访问它的唯一方法是在执行验证后放弃 formencode 并恢复到 request.POST['your_file']
。
我做错了什么?
<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'])
That's my code. form_result['your_file']
is a Unicode object. Because of this, I can't read the file or do any 'file' related tasks. The only way I've found to access it is to abandon formencode after it's performed its validation and revert to request.POST['your_file']
.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论