如何将Base64映像附加到Active Storage对象? [英] How to attach Base64 image to Active Storage object?
问题描述
我可以附加光盘上的png图像,并且一切正常:
I can attach a png image from disc by and everything works just fine:
obj.attachment.attach(
io: File.open('dog.png'),
filename: "image_name",
content_type: "image/png"
)
但是当我保存通过以下方式编码为String
之类的String
的Base64 png图像时,给出像很小的空正方形这样的结果是行不通的:
But it doesn't work giving result like too tiny empty square when I save a Base64 png image which encoded into String
something like that "data:image/png;base64,iVB**..REST OF DATA..**kSuQmCC"
by:
obj.attachment.attach(
io: StringIO.new(encoded_base_sixty_four_img),
filename: "image_name",
content_type: "image/png"
)
我也尝试对其进行解码,但是给出了相同的错误:
Also I tried to decoded it but gives the same error:
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
,但也无法给出空白图像结果:
Also tried writing this decoded value into a File
but nothing worked too giving blank image result:
file = file.write(decoded_base_sixty_four_img)
obj.attachment.attach(
io: file,
filename: "image_name",
content_type: "image/png"
)
还有其他想法吗?
推荐答案
感谢@tadman,当我将其剥离后,Base64.decode64
无法处理data:image/png;base64,
部分.
Thanks to @tadman, data:image/png;base64,
part can't be handled by Base64.decode64
when I stripped it off everything worked fine.
这篇关于如何将Base64映像附加到Active Storage对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!