【问题标题】:Rails - Download multiple files at same time using send_file in a each loopRails - 在每个循环中使用 send_file 同时下载多个文件
【发布时间】:2016-03-16 20:55:34
【问题描述】:

我想为我的数组中的每个值循环下载一个文件。

我的班级有两种方法:

def test
  testarray = ['value1', 'value2', 'value3']

  testarray.each do |value|
    download_file(value)
  end

end 

def download_file(value)

  send_file("public/downloads/test/" +  value  +".pdf", :filename => value + ".pdf", :type => "application/pdf", :diposition => "inline")

end

但是当我调用我的测试方法时,只下载了数组的最后一个元素。控制台打印以下内容:

 Sent file public/downloads/shapefiles/test1.pdf (0.1ms)
 Sent file public/downloads/shapefiles/test2.pdf (0.1ms)
 Sent file public/downloads/shapefiles/test3.pdf (0.1ms)
 Completed 200 OK in 2ms (ActiveRecord: 0.0ms)

但我的浏览器只下载最后一个文件,在本例中为test3.pdf

【问题讨论】:

    标签: ruby-on-rails sendfile


    【解决方案1】:

    send_file 实际上是指示服务器发送文件,而不是自己发送。例如,如果使用正确配置的 nginx - rails 只会发出 X-Accel-Redirect 标头,所有其他文件处理将由 nginx 完成。其他设置的行为大致相同(但没有反向代理文件上传仍然会阻止一些 ruby​​ 进程/线程)

    这就是为什么只发送最后一个文件。

    所以要下载多个文件,应该将它们打包成一个。您可以使用 nginx_mod_zip 使用类似于 X-accel-redirect 的技术动态创建 zip

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2015-11-21
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2019-03-24
      相关资源
      最近更新 更多