如何使用axlsx gem进行流式下载。我需要一种方法来下载大文件分块。时间将超过5分钟,并且我不想使用任何后台作业来完成此任务。我使用的是axlsx_rails gem
我想出的方法
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上面的解决方案对我不起作用。有人能告诉我我哪里错了吗?
发布于 2019-12-19 16:08:37
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发布于 2019-12-18 17:57:27
你真的想由你的用户生成文件触发器吗?这可能会打开DOS攻击载体。您应该考虑使用后台工作来准备这些文件。
其余的应该在你的the服务器中处理,而不是通过rails或rack。
https://stackoverflow.com/questions/59389318
复制相似问题