如何在rails 3中通过ftp写入csv文件?

如何在rails 3中通过ftp写入csv文件?

问题描述:

我想通过ftp写一个csv文件。这是我到目前为止:

I am trying to write to a csv file through ftp. Here is what i have so far:

require 'net/ftp'
require 'csv'

users = User.users.limit(5)

csv_string = CSV.generate do |csv|
  csv << ["email_addr", "first_name", "last_name"]

  users.each do |user|
    new_line = [user.email, user.first_name, user.last_name]
    csv << new_line
  end
end

csv_file = CSV.new(csv_string)

ftp = Net::FTP.new('**SERVER NAME**')
ftp.login(user = "**USERNAME**", passwd = "**PASSWORD**")
ftp.storbinary('STOR ' + 'my_file.csv', csv_file)
ftp.quit()

我得到错误的参数数for 3)。当我将行更改为ftp.storbinary('STOR'+'my_file.csv',csv_file,1024)它说错误的参数数量(1为0)。我也试过使用storlines,但是给了我错误。有人对如何处理这个问题有任何想法吗?

I get the error "wrong number of arguments (2 for 3)". When i change the line to ftp.storbinary('STOR ' + 'my_file.csv', csv_file, 1024) it says "wrong number of arguments (1 for 0)". I've also tried using storlines instead, but that gave me errors also. Does anybody have any ideas on how to handle this?

require 'net/ftp'



登录FTP服务器



Login to the FTP server

ftp = Net::FTP.new('ftp.sample.com', 'test', 'pass')



OR



OR

ftp = Net::FTP.new('ftp.sample.com')
ftp.login('test', 'pass')



切换到所需的目录



Switch to the desired directory

ftp.chdir('source/files')



获取我们需要的文件并将其保存到我们的'ftp_photos'目录



Get the file we need and save it to our 'ftp_photos' directory

ftp.getbinaryfile('photos_2009-03-29.zip', 'ftp_photos/photos.zip')



我们完成了,所以我们需要关闭连接。



We're done, so we need to close the connection

ftp.close

http://travisonrails.com/2009/03/29/ruby-net-ftp-tutorial

这将帮助你。