I have a bit of code in our app that downloads binary files and writes them to Tempfiles. Some testing led to the following exception:
An Encoding::UndefinedConversionError occurred in analyses#create: "\x90" from ASCII-8BIT to UTF-8 app/models/analysis/malware.rb:193:in `get_file'
Line 193 looked basically like this:
tempfile = Tempfile.new('tmp') begin tempfile.write(uploader.file.read) ensure tempfile.close end
I actually didn't want it converted to UTF-8, so a quick perusal of the Tempfile docs told me that I just needed to specify the encoding when I initialized the Tempfile.
So I changed the first line to:
tempfile = Tempfile.new('tmp', :encoding => 'ascii-8bit')
And everything was kosher.