【问题标题】:Streaming download with axlsx使用 axlsx 流式下载
【发布时间】:2020-04-10 20:18:50
【问题描述】:

如何使用 axlsx gem 进行流式下载。我需要一种方法来分块下载大文件。时间将超过 5 分钟,我不想为此任务使用任何后台作业。我正在使用axlsx_railsgem

我想出的方法

def download
  headers["Content-Type"] = "text/xlsx"  
  headers["Content-disposition"] = "attachment; filename=\"#{file_name}\""  
  headers['X-Accel-Buffering'] = 'no' 
  headers["Cache-Control"] ||= "no-cache" 
  headers.delete("Content-Length")  
  @result =  FetchRecord.new(data)
  response.status = 200
  render xlsx: "file_download.xlsx.axlsx", filename: "#{file_name.camelize}.xlsx",locals: {result: @result}
end

上述解决方案对我不起作用。谁能告诉我哪里出错了

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5.2 axlsx


    【解决方案1】:
    def download
      @result =  FetchRecord.new(data)
      @file = ActionView::Base.new(Rails.configuration.paths["app/views"]).render(handlers: [:axlsx], template: "model_name/download.xlsx", locals: {result: @result})
      respond_to do |format|
          format.xlsx { send_file @file, disposition: "attachment", filename: "file_download.xlsx"}
      end
    end
    

    【讨论】:

      【解决方案2】:

      您真的要生成由您的用户触发的文件吗?这可能会打开一个 DOS 攻击向量。您应该考虑准备这些文件的后台作业。

      其余的应该在您的网络服务器中处理,而不是通过导轨或机架。

      【讨论】:

      • 就我而言,我不必担心 DOS 攻击向量
      • 为什么要投反对票? DOS 在这里是一个问题,因为 axlsx 会消耗大量 RAM 并阻塞进程或线程,具体取决于您的实现。为此,您至少应该考虑一个带有 fetch_or_generate 方法的 ExcelCacheFile 类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2017-04-11
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多