Ruby-Rails 直接向客户端提供 ftp 文件 [英] Ruby-Rails serve ftp file direct to client

查看:17
本文介绍了Ruby-Rails 直接向客户端提供 ftp 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ruby​​ 和 rails 的新手,所以请原谅我的问题......我想知道的是,如何使用 ruby​​ 从 ftp 服务器获取文件而不将文件保存在我的 rails 应用程序硬盘驱动器上(将文件数据直接流式传输到客户端).我正在使用 ruby​​ Net/FTP 类.

I am new to ruby and to rails, so excuse my question... . What i want to know is, how to take a file from a ftp server with ruby without saving the file on my rails application harddrive (streaming the filedata direct to the client). I am working with the ruby Net/FTP class.

使用来自 Net/FTP 类的方法retrbinary",我有以下片段:

With the method "retrbinary" from the Net/FTP class i have the following snippet:

ftp.retrbinary('RETR ' + filename, 4096) { |data|
  buf << data
}

在我的 Rails 视图中,我可以执行以下操作:

In my rails view i can do something like this:

send_data(buf)

那么我如何将这两者结合起来.我不知道如何实例化缓冲区对象,填充流并将其提供给用户.有人知道怎么做吗?

So how do i combine these two. I dont know how to instanziate a buffer object, fill in the stream and than serve it to the user. Has anybody an idea how to do this?

推荐答案

非常感谢您的支持!你的帖子让我继续.喝了几杯咖啡后,我找到了一个可行的解决方案.实际上我正在执行以下操作,这对我有用:

thank you very much for your support! Your post get me going on. After some cups of coffee i found a working solution. Actually i am doing the following, which works for me:

def download_file
  filename = params[:file]
  raw = StringIO.new('')
  @ftp.retrbinary('RETR ' + filename, 4096) { |data|
    raw << data
  }
  @ftp.close
  raw.rewind
  send_data raw.read, :filename => filename
end

我将在生产环境中进行测试(现实生活中的情况).如果这还不够好,我必须使用 NFS 挂载.

I will test this in production(real life situation). If this is not working well enough, i have to use a NFS mount.

这篇关于Ruby-Rails 直接向客户端提供 ftp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆