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

发布于 2024-11-23 15:39:21 字数 697 浏览 1 评论 0 原文

我正在尝试通过 ftp 写入 csv 文件。这是我到目前为止所得到的:

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()

我收到错误“参数数量错误(2 为 3)”。当我将行更改为 ftp.storbinary('STOR ' + 'my_file.csv', csv_file, 1024) 时,它显示“参数数量错误(1 代表 0)”。我也尝试过使用 storlines 代替,但这也给了我错误。有人对如何处理这个问题有任何想法吗?

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()

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-11-30 15:39:21

在行

ftp.storbinary('STOR ' + 'my_file.csv', csv_file)

csv_file 中需要是一个实际的 File 对象,而不是另一种对象。

> (来自 ruby​​ 核心)

storbinary(cmd, file, blocksize, rest_offset = nil) { |data| ... }


将连接置于二进制(图像)模式,发出给定的服务器端
命令(例如“STOR myfile”),并发送名为 file 的文件的内容
到服务器。如果给出了可选块,它还会向其传递数据,在
块大小的字符块。

In the line

ftp.storbinary('STOR ' + 'my_file.csv', csv_file)

csv_file needs to be an actual File object, not another kind of object.

> (from ruby core)

storbinary(cmd, file, blocksize, rest_offset = nil) { |data| ... }


Puts the connection into binary (image) mode, issues the given server-side
command (such as "STOR myfile"), and sends the contents of the file named file
to the server. If the optional block is given, it also passes it the data, in
chunks of blocksize characters.

残月升风 2024-11-30 15:39:21
require 'net/ftp'

登录到 FTP 服务器

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

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

切换到所需目录

ftp.chdir('source/files')

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

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

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

ftp.close

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

这会对您有所帮助。

require 'net/ftp'

Login to the FTP server

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

OR

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

Switch to the desired directory

ftp.chdir('source/files')

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

This will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文