Skip to content

Commit 32861d1

Browse files
committed
✨change send_data to send_file call, so it will benifit from upstream thruster X-Sendfile function.
1 parent 48df685 commit 32861d1

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

app/controllers/employees_controller.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'csv'
2+
require 'tempfile'
3+
require 'rack/body_proxy'
24

35
class EmployeesController < ApplicationController
46
include Pagy::Backend
@@ -43,7 +45,21 @@ def report
4345
csv << values
4446
end
4547
end
46-
send_data "\xEF\xBB\xBF" << csv_res
48+
report_filename = 'user_report.csv'
49+
temp_file = Tempfile.new(['user_report', '.csv'], Rails.root.join('tmp'))
50+
temp_file.binmode
51+
temp_file.write("\xEF\xBB\xBF")
52+
temp_file.write(csv_res)
53+
temp_file.flush
54+
55+
send_file temp_file.path,
56+
filename: report_filename,
57+
type: 'text/csv; charset=utf-8',
58+
disposition: 'attachment'
59+
60+
response.body = Rack::BodyProxy.new(response.stream) do
61+
temp_file.close!
62+
end
4763
end
4864
end
4965
end

0 commit comments

Comments
 (0)