有没有办法使用 python formencode 验证文件大小?

发布于 2025-01-05 17:25:29 字数 232 浏览 3 评论 0原文

我想使用 formencode 在我的 Pyramid 应用程序中验证上传文件的大小。据我了解,我需要创建一个从 formencode.validators.FormValidator) 继承的类并将其放入 chained_validators 中。但我无法找到在 validate_python 方法中检查上传文件大小的方法。有可能吗?

预先感谢,伊万。

I'd like to validate uploaded file's size in my Pyramid application using formencode. As far as I understand, I need to create a class inherited from formencode.validators.FormValidator) and put it to chained_validators. But I can't figure out a way to check the uploaded file's size in the validate_python method. Is it even possible?

Thanks in advance, Ivan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2025-01-12 17:25:29

您可以在文件对象本身上使用 len() (在验证器中)来检查文件大小,因为它计算字节数。

size = len(fileObj)

You can use len() (in the validator) on the file object itself to check the file size, since its counting the bytes.

size = len(fileObj)
成熟稳重的好男人 2025-01-12 17:25:29

当然有 - 虽然我用涡轮齿轮做到了这一点,但它也应该适用于金字塔:

class MyFileValidator(FancyValidator):
    def _to_python(self, value, state):
        max_size = 10*1024*1024

        payload = value.file.read(max_size+1)

        # rewind so that the application can access the content
        value.file.seek(0)

        if len(payload) == max_size:
            raise Invalid(u"The file is too big (>10MB)", value, state)

        return value


class MySchema(Schema):
    my_file = MyFileValidator(not_empty=True)

请注意,不需要读取整个数据(请参阅其他答案) - 我这样做是为了进一步的内容验证。

Sure there is - although I did that with turbogears, it should work with pyramid as well:

class MyFileValidator(FancyValidator):
    def _to_python(self, value, state):
        max_size = 10*1024*1024

        payload = value.file.read(max_size+1)

        # rewind so that the application can access the content
        value.file.seek(0)

        if len(payload) == max_size:
            raise Invalid(u"The file is too big (>10MB)", value, state)

        return value


class MySchema(Schema):
    my_file = MyFileValidator(not_empty=True)

Note that read() ing the whole data should not be necessary (see other answer) - I did that for further content validation.

尬尬 2025-01-12 17:25:29

另一种方法:

class CheckFileSize(formencode.validators.FormValidator):
    __unpackargs__ = ('upload_field', 'max_file_size')

    def validate_python(self, value_dict, state):
        log.info('test')
        if value_dict.get(self.upload_field) is None:
            return value_dict
        fileobj = getattr(value_dict.get(self.upload_field), 'file', None)
        fileobj.seek(0, os.SEEK_END)
        if int(fileobj.tell()) > int(self.max_file_size):
            raise formencode.Invalid(
                _('File too big'),
                value_dict, state,
                error_dict={self.upload_field:
                    formencode.Invalid(_('File too big'), value_dict, state)})
        return value_dict

class CreateNewCaseForm(formencode.Schema):
    ...
    chained_validators = [
        CheckFileSize('file', max_upload_size),
    ]

Another way to do it:

class CheckFileSize(formencode.validators.FormValidator):
    __unpackargs__ = ('upload_field', 'max_file_size')

    def validate_python(self, value_dict, state):
        log.info('test')
        if value_dict.get(self.upload_field) is None:
            return value_dict
        fileobj = getattr(value_dict.get(self.upload_field), 'file', None)
        fileobj.seek(0, os.SEEK_END)
        if int(fileobj.tell()) > int(self.max_file_size):
            raise formencode.Invalid(
                _('File too big'),
                value_dict, state,
                error_dict={self.upload_field:
                    formencode.Invalid(_('File too big'), value_dict, state)})
        return value_dict

class CreateNewCaseForm(formencode.Schema):
    ...
    chained_validators = [
        CheckFileSize('file', max_upload_size),
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文