Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t将图像上载到活动存储器_Ruby On Rails_Image_Activerecord_Blob - Fatal编程技术网

Ruby on rails Can';t将图像上载到活动存储器

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

我无法使用ActiveStorage存储base64文件,我正在从客户端接收base64字符串

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
它起作用了:)谢谢,但我觉得它不直接起作用真的很奇怪。它起作用了:)谢谢,但我觉得它不直接起作用真的很奇怪。