我需要对不是图像的文件上传进行一些后期处理 – 在回形针中我可以有一个自定义的后处理器,但我找不到任何方法在carrierwave中执行此操作.
Ruby 1.9.3,Rails 3.2.7和CarrierWave 0.6.2.
解决方法
OP的问题是如何处理不是图像的文件.
请在GitHub上查看此源文件:
carrierwave/lib/carrierwave/uploader/processing.rb并查看评论.
您将创建自己的CarrierWave上传器子类并将其安装在您的模型中,如下所示:
def MyModel < ActiveRecord::Base # this is where the uploaded file will be available in your model,# as a `MyUploader` instance: # mount_uploader :uploaded_file,MyUploader ... end
请注意,它安装在ActiveRecord属性:uploaded_file上.
这意味着当您从模型访问:uploaded_file时,您将获得上载的特定文件的CarrierWave上传器的实例.
您可以在上传器中简单地定义处理,如下所示:
class MyUploader < CarrierWave:Uploader::Base process :my_custom_processing => [param1,param2,param3] def my_custom_processing(param1,param3) ... # e.g. you Could call a method which is defined elsewhere,# which operates on a file: my_nifty_file_processor( self.uploaded_file ) # # or you Could just do this directly: uploaded_data = self.uploaded_file.read ... end end
在my_nifty_file_processor中,您只需对传入的对象调用read即可读取该文件.
CarrierWave允许您在任何上传器实例(=上传文件的任何实例)上调用读取,它将读取该文件.
有时您需要访问上传文件的上传器中的ActiveRecord模型.
self.model