2017-04-04 47 views
0

我ENV:轨SEND_DATA无法处理大文件(2G +)

  • 的Mac:10.12.4
  • 内存:16G
  • 红宝石:2.1.4
  • 轨道:3.2.22.5
  • Web服务器:薄1.7.0

当文件大小为2G下,一切顺利。

class ItemListsController < ApplicationController 
... 
    send_data IO.read(zip_path), :type => 'application/zip', 
     :disposition => 'attachment', 
     :filename => file_name 

然而,当文件的大小大于2G,异常升高:

Errno::EINVAL: Invalid argument @ io_fread 

我试图用rubyzip到输出流,而不是:

compressed_filestream = Zip::OutputStream.write_buffer do |zos| 
     files.each do |file| 
     zos.put_next_entry file[1] 
     zos.write File.open(file[0], 'r').read 
     end 
    end 

    compressed_filestream.rewind 

    send_data compressed_filestream.read, :type => 'application/zip', 
       :disposition => 'attachment', 
       :filename => file_name 

异常与进一步详细提出:

Unexpected error while processing request: integer 2206004964 too big to convert to `int' 
/Users/karl/.rvm/gems/[email protected]/gems/eventmachine-1.0.3/lib/em/connection.rb:328:in `send_data' 

似乎send_data会将整个文件读入内存然后发回数据。

我原来的计划是找到一些方法,以提供“缓冲”,所以SEND_DATA将从缓冲区,而不是读取整个文件读取,但无法找到API这样的选项

https://apidock.com/rails/ActionController/DataStreaming/send_data

任何想法会不胜感激。

感谢。

回答

0

您可能想用send_file代替。根据documentation

发送文件,默认一次流4096字节。这种方式 整个文件不需要立即被读入内存。这使得 甚至可以发送大文件。您可以选择关闭 流式传输并一次发送整个文件。

+0

感谢您的回复。我试过使用send_file,但仍然不起作用。大文件问题(大于2206004964字节)仍然存在。我尝试了不同的Web服务器:Thin和Puma(3.8.2),仍然一样。 –

+0

你可以编辑你的问题并发送新的代码与send_file? – Jeremie