ruby-on-rails - 如何将 Base64 图像附加到 Active Storage 对象?

标签 ruby-on-rails ruby base64 rails-activestorage ruby-on-rails-5.2

我可以从光盘附加一个 png 图像,一切正常:

obj.attachment.attach(
  io: File.open('dog.png'),
  filename: "image_name",
  content_type: "image/png"
)

但是当我保存编码为 String 的 Base64 png 图像时,它不会给出像太小的空方块这样的结果。类似的东西 "data:image/png;base64,iVB**..REST OF DATA..**kSuQmCC"经过:
obj.attachment.attach(
  io: StringIO.new(encoded_base_sixty_four_img),
  filename: "image_name",
  content_type: "image/png"
)

我也试图解码它,但给出了同样的错误:
decoded_base_sixty_four_img = Base64.decode64(encoded_base_sixty_four_img)
obj.attachment.attach(
  io: StringIO.new(decoded_base_sixty_four_img),
  filename: "image_name",
  content_type: "image/png"
)

还尝试将此解码后的值写入 File但没有任何效果,给出空白图像结果:
file = file.write(decoded_base_sixty_four_img)
obj.attachment.attach(
  io: file,
  filename: "image_name",
  content_type: "image/png"
)

那么还有其他想法吗?

最佳答案

感谢@tadman,data:image/png;base64,部分无法由 Base64.decode64 处理当我把它脱下来时,一切正常。

关于ruby-on-rails - 如何将 Base64 图像附加到 Active Storage 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61157933/

相关文章:

python - Python 变得更面向对象了吗?

java - 有没有办法将 tiff 图像转换为 Base64 而不将完整文件加载到内存中

ruby-on-rails - Azure 上的 Ruby on Rails

iphone - Authlogic 和 iPhone 应用程序登录

ruby-on-rails - 如何解析文本以获得所有提及者?

html - 在 Nokogiri 中禁用纠错

ruby-on-rails - 嵌套参数,具有一对一的多态性

ruby-on-rails - Rails 应用程序各部分的可视化表示

image - 在 AngularJS 中使用包含 base64 数据的变量加载图像 src

node.js - 使用 Nodejs 检索存储在 Mongodb 中的图像