Why do I get a string encoding issue "\xE2" from ASCII-8BIT to UTF-8? Why do I get a string encoding issue "\xE2" from ASCII-8BIT to UTF-8? ruby ruby

Why do I get a string encoding issue "\xE2" from ASCII-8BIT to UTF-8?


The error message is actually being thrown on the file write, not by your encode/decode inside the params, because Ruby is trying to apply default character encoding on file.write. To prevent this, the quickest fix is to add the b flag when you open the file

file = File.new base_path + filename, 'wb+'file.write Base64.decode64( attachment.source['Content'] )

That's assuming the incoming attachment is encoded in Base64, as your code implies (I have no way to verify this). The Base64 encoding stored inside attachment.source['Content'] should be the same bytes in ASCII-8BIT and UTF-8, so there is no point converting it inside the call to decode64.