Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoMapper Error "\xFF" from ASCII-8BIT to UTF-8 when storing a file in DB

I have a problem to Store file (from a remote API) in MongoDB, i'm with Ruby 1.9

class Foo
  include ::MongoMapper::Document
  key :bar, String
end

I get the following error : "\xFF" from ASCII-8BIT to UTF-8

I tried the following things :

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

Well any of them work, I get an error while save call ...

I look on the net but I didn't find any clear responce (or at least solving my probleme)... Any idea what i'm supposed to do to be able to store it ?

like image 623
ProxyGear Avatar asked Dec 02 '25 16:12

ProxyGear


1 Answers

If you are using TempFile, make sure you put it into binary mode. An example:

file = Tempfile.new('tmp').tap do |file|
  file.binmode # must be in binary mode
  file.write image.to_blob
  file.rewind
end
like image 153
sandstrom Avatar answered Dec 06 '25 01:12

sandstrom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!