MongoMapper在DB中存储文件时,从ASCII-8BIT到UTF-8的错误"\ xFF"

Pro*_*ear 3 encoding mongodb mongomapper ruby-on-rails-3 ruby-1.9

我在MongoDB中存储文件(来自远程API)有问题,我使用的是Ruby 1.9

class Foo
  include ::MongoMapper::Document
  key :bar, String
end
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:"\ xFF"从ASCII-8BIT到UTF-8

我尝试了以下事项:

foo.bar = pdf_data.encode('UTF-8')
foo.bar = pdf_data.force_encoding('UTF-8')
foo.bar = pdf_data.ensure_encoding('UTF-8',
  :external_encoding  => :sniff,
  :invalid_characters => :transcode
) # with github.com/Manfred/Ensure-encoding
Run Code Online (Sandbox Code Playgroud)

好吧,其中任何一个都有效,我在保存电话时收到错误...

我在网上看,但我没有找到任何明确的反应(或至少解决我的问题)......任何想法我应该做什么来能够存储它?

san*_*rom 6

如果您正在使用TempFile,请确保将其置于二进制模式.一个例子:

file = Tempfile.new('tmp').tap do |file|
  file.binmode # must be in binary mode
  file.write image.to_blob
  file.rewind
end
Run Code Online (Sandbox Code Playgroud)