[通过以下方式尝试将字符串/解压缩的文件写入Tempfile:
temp_file = Tempfile.new([name, extension])
temp_file.write(unzipped_io.read)
当我对图像执行此操作时会引发以下错误:
Encoding::UndefinedConversionError - "\xFF" from ASCII-8BIT to UTF-8
[进行研究时,我发现这是由于Ruby尝试使用默认编码(UTF-8)编写文件而引起的。但是该文件应写为二进制文件,因此它会忽略任何文件特定的行为。
编写常规的File
,您可以按照以下步骤进行操作:
File
如何在File.open('/tmp/test.jpg', 'rb') do |file|
file.write(unzipped_io.read)
end
中执行此操作
我在一个旧的Ruby论坛帖子中遇到了该解决方案,所以我想在这里分享它,使人们更容易找到:
另一种选择是temp_file = Tempfile.new([name, extension])
temp_file.binmode
temp_file.write(unzipped_io.read)
[IO.binwrite(path, file_content)
将选项传递给接受Tempfile.new
的File.open
,特别是: