如何在rails 3中通过ftp写入csv文件?
我正在尝试通过 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 代替,但这也给了我错误。有人对如何处理这个问题有任何想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在行
csv_file 中需要是一个实际的 File 对象,而不是另一种对象。
> (来自 ruby 核心)
storbinary(cmd, file, blocksize, rest_offset = nil) { |data| ... }
将连接置于二进制(图像)模式,发出给定的服务器端
命令(例如“STOR myfile”),并发送名为 file 的文件的内容
到服务器。如果给出了可选块,它还会向其传递数据,在
块大小的字符块。
In the line
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.
登录到 FTP 服务器
或
切换到所需目录
获取我们需要的文件并将其保存到我们的“ftp_photos”目录
我们完成了,所以我们需要关闭连接
http://travisonrails.com/2009/03/29/ruby-net-ftp-tutorial
这会对您有所帮助。
Login to the FTP server
OR
Switch to the desired directory
Get the file we need and save it to our 'ftp_photos' directory
We're done, so we need to close the connection
http://travisonrails.com/2009/03/29/ruby-net-ftp-tutorial
This will help you.