有没有办法使用 python formencode 验证文件大小?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在文件对象本身上使用 len() (在验证器中)来检查文件大小,因为它计算字节数。
You can use len() (in the validator) on the file object itself to check the file size, since its counting the bytes.
当然有 - 虽然我用涡轮齿轮做到了这一点,但它也应该适用于金字塔:
请注意,不需要读取整个数据(请参阅其他答案) - 我这样做是为了进一步的内容验证。
Sure there is - although I did that with turbogears, it should work with pyramid as well:
Note that read() ing the whole data should not be necessary (see other answer) - I did that for further content validation.
另一种方法:
Another way to do it: