CarrierWave:为多种类型的文件创建 1 个上传器
我想为多种类型的文件(图像、pdf、视频)创建 1 个上传程序
对于每种 content_type 都会执行不同的操作
我如何定义文件的 content_type?
例如:
if image?
version :thumb do
process :proper_resize
end
elsif video?
version :thumb do
something
end
end
I want to create 1 uploader for multiple types of files (images, pdf, video)
For each content_type will different actions
How I can define what content_type of file?
For example:
if image?
version :thumb do
process :proper_resize
end
elsif video?
version :thumb do
something
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了这个,它看起来像是如何解决此问题的示例:https://gist.github.com/995663。
当您调用
mount_uploader
时,上传器首先被加载,此时if image?
或elsif video?
将不起作用,因为尚未定义要上传的文件。您需要在实例化类时调用这些方法。我上面给出的链接的作用是重写
process
方法,以便它采用文件扩展名列表,并且仅当您的文件与这些扩展名之一匹配时才进行处理,然后您可以使用它来调用什么过去用于处理
版本,如果您使用的是 >0.5.4,则 Carrierwave wiki 上有一个页面允许您有条件地处理版本。 https://github.com/jnicklas/rierwave/wiki/How-to% 3A-执行条件处理。您必须将版本代码更改为如下所示:
I came across this, and it looks like an example of how to solve this problem: https://gist.github.com/995663.
The uploader first gets loaded when you call
mount_uploader
, at which point things likeif image?
orelsif video?
won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead.What the link I gave above does, is rewrite the
process
method, so that it takes a list of file extensions, and processes only if your file matches one of those extensionsYou can then use this to call what used to be process
For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing. You'll have to change the version code to look like this: