FormEncode 将我上传的文件作为 Unicode 对象返回 - 如何修复?

发布于 2024-10-04 15:33:41 字数 882 浏览 0 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文