在服务器上保存本地附件

时间:2013-08-01 01:07:41

标签: ruby-on-rails ruby-on-rails-3.2 paperclip

虽然paperclip gem通常用于轻松上传文件,但我在服务器端也有类似的要求。我在服务器上生成了一个图像,并希望按照回形针选项中的配置将其保存为各种样式。

我已经生成了迁移,我的模型如下所示:

attr_accessible :genimg
has_attached_file :genimg
  styles: { mini: '48x48>', small: '100x100>',
            product: '240x240>', large: '600x600>' },
  default_style: :product
  url: '/images/:basename.:extension',
  path: ':rails_root/app/assets/images/:basename.:extension'
  convert_options: { all: '-strip -auto-orient' }

有没有办法可以使用回形针以各种格式保存我的本地图像。我怎么能这样做?如果那是不可能的,那么另一种可能的尝试方法是什么?

1 个答案:

答案 0 :(得分:0)

class YourController < ActionController::Base
  def your_action
    @image = Image.find 1 # (might be other way how you get the asset)

    file_path = '/path/to/your/file'
    file      = File.open(file_path, 'r')

    @image.asset = file
    @image.save
  end
end
在控制台中你可能会这样做:

Image.new(:data => File.new(path_to_your_file, "r"))