2017-04-04 49 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