我可以从光盘附加一个 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/