Ruby 1.9 + Net::FTP => Encoding::UndefinedConversionError

橙三吉。 提交于 2020-01-15 11:25:08

问题


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:


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



来源:https://stackoverflow.com/questions/9905268/ruby-1-9-netftp-encodingundefinedconversionerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!