Ruby on rails Can';t将图像上载到活动存储器
我无法使用ActiveStorage存储base64文件,我正在从客户端接收base64字符串Ruby on rails Can';t将图像上载到活动存储器,ruby-on-rails,image,activerecord,blob,Ruby On Rails,Image,Activerecord,Blob,我无法使用ActiveStorage存储base64文件,我正在从客户端接收base64字符串 params["image"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAREAAABMCAYAAABK84MTAAAABHNCSVQICAgIfAhkl0RVh" 当我尝试连接它时,我得到: ActiveSupport::MessageVerifier::InvalidSignature(ActiveSupport::MessageVeri
params["image"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAREAAABMCAYAAABK84MTAAAABHNCSVQICAgIfAhkl0RVh"
当我尝试连接它时,我得到:
ActiveSupport::MessageVerifier::InvalidSignature(ActiveSupport::MessageVerifier::InvalidSignature):
我已经学习了很多教程,首先尝试解码:
decoded_image = Base64.decode64(params["image"])
post.image.attach(decoded_image)
以及删除数据:image/png;base64是字符串的一部分,包含:
decoded_image = Base64.decode64(params["image"]['data:image/png;base64,'.length .. -1])
然后,如果我直接从具有以下内容的文件中附加图像,但没有成功:
file = open("image.png")
post.image.attach(io: file, filename: "post.png")
它工作得很好,因此我认为我的错误是在解析字符串时,我不确定它是否会工作,但是尝试一下这种方法,使用
Tempfile
创建一个临时文件:
encoded_image = params["image"]['data:image/png;base64,'.length .. -1]
decoded_image = Base64.decode64(encoded_image)
file = Tempfile.new
file.binmode
file.write(decoded_image)
file.rewind
post.image.attach(
io: file,
filename: "post.png" # The name of the file should be received from paramaters, as well
)
file.close
file.unlink
我不确定它是否有效,但请尝试使用
Tempfile
创建临时文件的方法:
encoded_image = params["image"]['data:image/png;base64,'.length .. -1]
decoded_image = Base64.decode64(encoded_image)
file = Tempfile.new
file.binmode
file.write(decoded_image)
file.rewind
post.image.attach(
io: file,
filename: "post.png" # The name of the file should be received from paramaters, as well
)
file.close
file.unlink
它起作用了:)谢谢,但我觉得它不直接起作用真的很奇怪。它起作用了:)谢谢,但我觉得它不直接起作用真的很奇怪。