Rnd*_*der 5 ruby base64 jpeg binmode
我有一个 base64 编码的图像数据。我正在粘贴前几个字符
string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD /2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopG R8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgo......'
Run Code Online (Sandbox Code Playgroud)
我正在用 ruby 跟踪它
decoded_string=Base64.decode64 string
output_file = Tempfile.new(['image','.jpeg'])
output_file.binmode
output_file.write image
Run Code Online (Sandbox Code Playgroud)
在此之后,当我打开“image.jpeg”时,出现错误
Error interpreting JPEG image file (Not a JPEG file: starts with 0x75 0xab)
Run Code Online (Sandbox Code Playgroud)
我也试过
File.open('a.jpeg', 'wb') do|f|
f.write decoded_string
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我也遇到了同样的错误。
我究竟做错了什么?
File.open('shipping_label.gif', 'wb') do|f|
f.write(Base64.decode64(base_64_encoded_data))
end
Run Code Online (Sandbox Code Playgroud)
这个答案来自:How to save a base64 string as an image using ruby