Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require './lib/exporters/experiment_exporter'
module Api
module V1
class ExperimentExporterController < Api::ApiController
include ActionController::Live
def show
authorize! :read, :experiment_exporter
# experiment_id = params[:id]
# exporter = Exporters::ExperimentExporter.new(experiment_id)
# writer = StreamWriter.new(response, "experiment.csv")
# exporter.export2(writer)
# send_file(file,
# :filename => "experiment.csv",
# :type => "text/csv")
response.headers['Content-Type'] = 'text/event-stream'
10.times {
puts "streamuje"
response.stream.write "This is a test Messagen"
sleep 1
}
response.stream.close
end
class StreamWriter
def initialize(response, filename)
@response = response
@filename = filename
@first_write = true
end
def write(data)
if @first_write
puts ">>>>>>>>>>>>>>>> dodaje headery #{@first_read}"
@response.headers['Content-Type'] = 'Application/octet-stream'
@response.headers['Content-Disposition'] = 'attachment; filename="' + @filename + '"'
@response.headers['X-Accel-Buffering'] = 'no'
@first_write= false
end
puts ">>>>>>>>>> pisze do streama"
@response.stream.write data
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment