【问题标题】:failed to allocate memory while downloading large files in rails在 Rails 中下载大文件时分配内存失败
【发布时间】:2010-09-16 05:57:46
【问题描述】:

大家好, 我正在尝试使用 send_data 函数在 Rails 中下载大文件,但是 出现错误:分配内存失败,尝试分块下载时,仅获取块大小文件,下面是我的代码..

File.open(@containerformat.location,"rb"){|f| @data = f.read(8888)}
ext = File.extname(@containerformat.streamName)

if ext == '' 
  extension = File.extname(@containerformat.location)
  send_data(@data,:filename => @containerformat.name+extension,
  :disposition => 'attachment')
else   
  send_data(@data,:filename => @containerformat.streamName,
  :disposition => 'attachment')
end

我认为我无法使循环工作

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您正在将整个文件读入内存! 使用send_file,它使用内存友好的缓冲流。

    我还建议在这里使用 :x_sendfile,如果有适当的模块可用和配置,文件可以直接由前端服务器(Apache、nginx、lighttpd)提供。这提供了非常有效的下载并防止慢速客户端阻塞 Rails 实例。

    阅读“X-Sendfile”标头。 http://tn123.ath.cx/mod_xsendfile/

    【讨论】:

    • 以及如何使用 send_data 函数分块发送数据??
    • send_data 和 send_file 会立即将字节发送到流中,因此您不能在操作中调用它们两次,请使用 send_file 附加您的代码 - 它已经完成了您尝试使用“chunked send_data”实现的目标
    • 意思是用上面的代码我不能实现分块数据下载,那么我如何下载大文件和使用send_data或send_file是必须的
    • 你能告诉我如何使用 send_file 来下载上面代码的文件我只是在上面的代码中更改 send_file 而不是 send_data
    • send_file @containerformat.location, :filename => "filename-here.ext" #location 应该是绝对的
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2013-06-16
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多