1
votes

i upgraded from Ruby 1.8.7 to 1.9.2 (and Rails 3.2.2) and have the problem that Net::FTP#gettextfile is throwing an Encoding::UndefinedConversionError

I'm trying to download a xml file which is encoded as utf-8. The dowload works fine if i use getbinaryfile, but i would like to understand why gettextfile is not working.

# -*- encoding : utf-8 -*-

# working
ftp = Net::FTP.open(host,user,password)
ftp.passive = true
ftp.getbinaryfile("some.xml","tmp/some.xml")

# not working
ftp = Net::FTP.open(host,user,password)
ftp.passive = true
# raises Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8
ftp.gettextfile("some.xml","tmp/some.xml") 

I know that i can pass the external and internal encoding if use File.open like this:

File.open("some.xml", "r:ASCI-8BIT:UTF-8")

but couldn't find an option like this for Net::FTP.

I also tried the block version of gettextfile, but this isn't working as well and gives the same error message.

File.open("some.xml", "w:ASCII-8BIT:UTF-8") do |file|
  ftp.gettextfile("some.xml") { |line| file.write line }
end

Has anyone an idea what is wrong here?

1

1 Answers

3
votes

Use ftp.getbinaryfile instead of ftp.gettextfile then it works again. :)