我在 MongoDB 中存储文件(来自远程 API)时遇到问题,我使用的是 Ruby 1.9
class Foo
include ::MongoMapper::Document
key :bar, String
end
我收到以下错误:“\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
好吧,它们中的任何一个都可以工作,我在保存调用时遇到错误...
我在网上查看,但没有找到任何明确的回应(或者至少解决了我的问题)... 知道我应该怎么做才能存储它吗?
最佳答案
如果您正在使用 TempFile
,请确保将其置于二进制模式。一个例子:
file = Tempfile.new('tmp').tap do |file|
file.binmode # must be in binary mode
file.write image.to_blob
file.rewind
end
关于ruby-on-rails-3 - 在数据库中存储文件时,从 ASCII-8BIT 到 UTF-8 的 MongoMapper 错误 "\xFF",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191875/