`write': "\xCF" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) while writing to file from url
Asked Answered
D

3

12

I am getting error:

write': "\xCF" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

from line:

open(uri) {|url_file| tempfile.write(url_file.read)}

relevant code is:

require 'tempfile'
require 'open-uri'
require 'uri'
..
uri = URI.parse(@download_link)
tempfile = Tempfile.create(file_name)
open(uri) {|url_file| tempfile.write(url_file.read)}`
..

It runs completely fine if I run it like ruby lib/file.rb, but gives error when I run it in rails environment: rails runner lib/file.rb.

Most questions with this error refer to gem installation scenarios. My guess that I have to include/update some gems, but have no idea which.

Drumfire answered 14/7, 2017 at 17:53 Comment(0)
S
17

Use force_encoding:

open(uri) {|url_file| tempfile.write(url_file.read.force_encoding("UTF-8"))
Spadefish answered 14/7, 2017 at 18:24 Comment(0)
C
16

Accepted answer is fine, but I think it is worth mentioning that You can also set encoding when creating/opening Tempfile, for example:

Tempfile.new("file.pdf", encoding: 'ascii-8bit') # or 'utf-8'
Combustion answered 7/11, 2017 at 8:48 Comment(0)
S
16

This should solve the problem.

data = URI.parse(@download_link).read
tempfile = Tempfile.create(file_name)
tempfile.binmode # This will help deal encoding problem with download files from the internet
tempfile.write(data)

binmode is binary mode

Secondhand answered 1/8, 2018 at 19:54 Comment(2)
This is the answer that addresses the real core of the issueIncorporeity
This answer did it for meTague

© 2022 - 2024 — McMap. All rights reserved.