2012-05-10 16 views
8

私はbase64でエンコードされたイメージファイルの文字列を持っています。現在、ファイルはテキストファイル(stringio.txt)として保存されファイルアップロードRailsを使ってPaperClipのBase64でエンコードされた文字列

has_attached_file :profile_pic, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => '/icon.jpg' 

私のユーザモデルでは

@driver = User.find(6) 
encoded_file = Base64.encode64(File.open('/pjt_path/public/test.jpg').read) 
decoded_file = Base64.decode64(encoded_file) 

@driver.profile_pic = StringIO.open(decoded_file) 
@driver.save 

である私はペーパークリップに

マイコントローラー・コードを使用して、それを保存する必要があります。しかし、私がJPGへの拡張を変更すると、それをイメージとして見ることができます。どのようにすれば、StringIOを使って正しく画像に名前を付けることができますか?

私が午前レール3.2、ルビー1.9.2、3.0.3クリップ

答えて

11

私はこの問題を使用して修正しました

encoded_file = Base64.encode64(File.open('/pjt_path/public/test.jpg').read) 
decoded_file = Base64.decode64(params[:encoded_image]) 
begin 
    file = Tempfile.new(['test', '.jpg']) 
    file.binmode 
    file.write decoded_file 
    file.close 
    @user.profile_pic = file 
    if @user.save 
    render :json => {:message => "Successfully uploaded the profile picture."} 
    else 
    render :json => {:message => "Failed to upload image"} 
    end 
ensure 
    file.unlink 
end 
+0

あなたのコードを投稿してください。私は非常にintrestingそれを見つけた! –

+1

@EmSta - ここに完全なコードを掲載しました。このコードは私のコントローラ機能に追加されています。あなたに必要なコードが何であるか教えてください –

+0

私は新しい質問を残します:http://stackoverflow.com/questions/17336788/paperclip-upload-anage-encoded-with-base64多分あなたは私を助けることができます@AmalKumarS –

関連する問題