0

ファイルが添付されたPictureというモデルがあります。私が使用しているソフトウェア:

*Rails 3
*Ruby 1.9.3
*Paperclip
*ImageMagick

私のモデルは:

class Picture < ActiveRecord::Base
    attr_accessible :picturefile,:picturefile_file_name,:picturefile_content_type,:picturefile_file_size,:picturefile_updated_at
    has_attached_file :picturefile,
    :styles => {
        :thumb=> "100x100#"},
        :storage => :s3,
        :s3_credentials => Rails.root.join('config','s3.yml'),
        :path => "/travels/:style/:id/:basename.:extension"
end

そして私のコントローラーにはこれがあります:

StringIO.open(Base64.decode64(params[:Route][:CoverPicture][:File][:EncodedContent])) do |data|
fotocov_t=Picture.create({
  :picturefile=>data
})
travel.pictures = [fotocov_t] #this assigns the new picture to a travel i created before the picture
end

ご覧のとおり、文字列をアップロードしていますが、アップロードしたいのは、でエンコードされた文字列によって作成された画像です。

params[:Route][:CoverPicture][:File][:EncodedContent]

複数のアプローチを試しましたが、テキストファイルを保存し続けています。

テキストファイルをアップロードする代わりに、ファイルを画像としてアップロードする方法を知っている人はいますか?

ありがとうございました :)

4

1 に答える 1

0

それを見つけた :)

bytes = Base64.decode64(params[:Route][:CoverPicture][:File][:EncodedContent])
img   = Magick::Image.from_blob(bytes).first

記録の作成時:

:picturefile=>img

rmagick が必要です。今、私は No handler で別の問題を抱えています...

しかし、情報を検索してアップロードします:)

于 2013-03-08T17:19:04.050 に答える