使用 Carrierwave 验证上传大小
在我们最新的应用程序中,我们需要处理一些上传,我之前使用过回形针,一切正常!但我们正在尝试 Carrierwave,它看起来很有希望,但是,我找不到如何验证附件,文档似乎没有任何相关信息,我们是否应该手动将其添加到通过自定义验证器的模型?
提前致谢!
In our latest application we need to process some uploads, I've worked with paperclip before and everything just works! but we're giving carrierwave a try, it looks promising but, I can't find how to validate the size of an attachment, it seems like the documentation doesn't have any information about it, should we add it manually to the model via a custom validator?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
github 上有一个 Wiki 条目: https:/ /github.com/jnicklas/rierwave/wiki/How-to%3A-Validate-attachment-file-size
There is a Wiki entry on github: https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Validate-attachment-file-size
我制作了一个活动模型 文件验证器 gem,用于检查 Carrierwave、PaperClip 的内容类型和文件大小验证, Drangonfly、Refile(希望它能与其他上传解决方案配合使用)。它根据文件的内容检测内容类型,并且具有媒体类型欺骗检测器。它在上传之前和之后都有效。
I've made an Active Model File Validators gem that checks content type and file size validation for Carrierwave, PaperClip, Drangonfly, Refile (hopefully it will work with other uploading solutions). It detects the content type based on the content of the file and it has a media type spoof detector. It works both before and after uploads.
从
1.0
版本开始,CarrierWave 具有内置文件大小验证功能。安装最新的
rierwave
gem添加方法
size_range
以提供最小大小和最大大小在模型中添加
validates_integrity_of
以验证文件大小(和内容类型) )的图像。Since
1.0
version CarrierWave has built-in file size validation.Install latest
carrierwave
gemAdd method
size_range
to provide a min size and a max sizeIn model add
validates_integrity_of
to valid a file size (and content type) of an image.这是我想出的解决方案 - 诀窍是我无法检查文件直接调整大小,因为如果文件未上传,则会产生 Fog RubyGem 炸弹。我希望有一种更简洁的方式来询问 CarrierWave 文件是否已上传。
Here is the solution that I came up with - the trick was that I couldn't check the file size directly as that made the Fog RubyGem bomb if the file hadn't been uploaded. I would expect there to be a cleaner way to ask CarrierWave if a file was uploaded.