23
19

More than 3 years have passed since last update.

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

Last updated at Posted at 2018-03-06

エンコード

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
23
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
19