Posted at

RailsでBase64化された画像を取り回すときのSnippet

More than 1 year has passed since last update.


エンコード

image = File.read(Rails.root.join('test', 'files', 'sample.jpg'))

b64_image = Base64.strict_encode64(image)


デコード

bin = Base64.decode64(b64_image)


Carrierwaveで保存

def image_from_base64(b64)

bin = Base64.decode64(b64)
file = Tempfile.new('img')
file.binmode
file << bin
file.rewind

self.image = file
end