简体   繁体   中英

Ruby on Rails - Encoding::UndefinedConversionError: “\xC3” from ASCII-8BIT to UTF-8

I have a process that fetches a flat file from a mainframe via FTP. This usually works fine for some files. In others, I get: Encoding::UndefinedConversionError: "\\xC3" from ASCII-8BIT to UTF-8

That's using Net::FTP's gettextfile method. Here is my code:

def find_file( position, value ) # => Value = CLKDRP03.txt, Forget the variable Position

    ftp = Net::FTP.new('IP') # => status 200
    ftp.login('user','pass') # => True

    files = ftp.list('*' + value + '*') # => Obtain the file

    if files[0] != nil

      str_file = files[0].split(" ").last # => Obtain the name of the file. In this case CLKDRP03.txt

      ftp.gettextfile(str_file,'data/input/' + str_file) # => ERROR!, str_file = CLKDRP03.txt
      # => data/input is the path where I put the files from FTP.
      return str_file

    else  
      return false
    end

end

If I comment the line of ftp.gettextfile , the error keeps coming.

Thanks in advance! Sorry for my english.

For anybody that are coming here from google this is how I fixed my encoding error.

right before you declare of open your file add this code

.force_encoding("UTF-8")

so before you declare the file to a variable like this :

csv_file = params[:file].read.force_encoding("UTF-8")

Hope this will help some since I had this problem for a long time, but now it works yay!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM