2013-03-07 47 views
0

我有一個名爲圖片的模型,並附上了一個文件。 我使用的軟件:上傳一個圖像,作爲一個編碼的字符串從一個發佈請求到S3與rails 3

*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] 

我已經嘗試了多個aproaches,但一直保存一個文本文件。

有誰知道我可以如何上傳文件作爲圖像,而不是上傳文本文件?

謝謝:)

回答

0

發現它:)

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

在創建記錄:

:picturefile=>img 

所需rmagick。 現在我有沒有處理的另一個麻煩...

但必應搜索和上傳信息:)

+0

製造它完成! 用於rmagick以獲得格式: 字節= Base64.decode64(PARAMS [:路線] [:CoverPicture] [:文件] [:EncodedContent]) IMG = Magick :: Image.from_blob(字節)。首先 之後: 數據= StringIO.new(Base64.decode64(~~~~) data.class.class_eval {attr_accessor:original_filename,:CONTENT_TYPE} data.original_filename = 「蓋」 + FMT data.content_type = 「image /」+ fmt And: :picturefile => data – ArcherDragon57 2013-03-08 17:58:43

相關問題