Last active
January 23, 2019 10:32
-
-
Save manojmj92/63ed977a7b3ee19470fbfa5d6994acb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In the controller#action | |
respond_to do |format| | |
format.csv { render_streaming_csv(generate_csv(column_names, records)) } | |
end | |
def render_streaming_csv(csv_enumerator) | |
# Delete this header so that Rack knows to stream the content. | |
headers.delete("Content-Length") | |
# Do not cache results from this action. | |
headers["Cache-Control"] = "no-cache" | |
# Let the browser know that this file is a CSV. | |
headers['Content-Type'] = 'text/csv' | |
# Do not buffer the result when using proxy servers. | |
headers['X-Accel-Buffering'] = 'no' | |
# Set the filename | |
headers['Content-Disposition'] = "attachment; filename=\"report.csv\"" | |
# Set the response body as the enumerator | |
self.response_body = csv_enumerator | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment