Bug #13738
closedActive Scaffold image upload using CarrierWave gem
Description
getting Encoding::UndefinedConversionError ("\xFF" from ASCII-8BIT to UTF-8): this error while uploding the image. and image params are like this:
"photo"=>#<ActionDispatch::Http::UploadedFile:0x007fbcc2925c80 @tempfile=#Tempfile:/tmp/RackMultipart20170712-7555-c67kvs.jpg, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name="record[photo]"; filename="1.jpg"\r\nContent-Type: image/jpeg\r\n">
Updated by k0kubun (Takashi Kokubun) over 7 years ago
- Status changed from Open to Third Party's Issue
Did you see https://github.com/carrierwaveuploader/carrierwave/issues/1404?
As far as I can see from your description, it's unlikely to be a bug in Ruby-side. Please confirm the issue and then could you report it to carrierwave or Rails?
Updated by duerst (Martin Dürst) over 7 years ago
karanamsandhya (Karanam Sandhya) wrote:
getting Encoding::UndefinedConversionError ("\xFF" from ASCII-8BIT to UTF-8): this error while uploding the image.
There's definitely no error in Ruby itself, in the encoding "ASCII-8BIT", only the lower half is convertible; the upper half is okay in the source, but undefined and so not convertible to any other encoding.
I don't know the CarrierWave gem. But if they are trying to convert image data (which very easily can contain a 0xFF byte, and for which a String with an encoding of ASCII-8BIT (alias: BINARY) is appropriate) to UTF-8, then they are definitely making a mistake. If the error happens for some other data (e.g. for some text data extracted from an image header), then I can understand that such an error happens. If the image format actually defines what encoding to use for the text, then String#force_encoding should be applied first. If not, I'd suggest to change the conversion from something like
string_with_unknown_encoding_labeled_as_ascii_8bit.encode('UTF-8')
to something like
string_with_unknown_encoding_labeled_as_ascii_8bit.encode('UTF-8', invalid: :replace)
which will convert 0xFF bytes (and similar) to '?'.