I have a function in the controller that accepts some specifications and generates a report about them. This user_report function is called in the view:
<% = submit_to_remote 'submit-button', "Export Report to Excel" ,: url => {: controller =>: reports ,: action =>: user_report ,: print_state => 'print'}%>
In reports_controller, I use the Spreadsheet plugin to create an Excel file in the user_report function. I want to use send_data to stream a file to a user without creating it on the server. The research I did shows that using StringIO is the way as shown below. Disappointingly, nothing happens when I call send_data. The plugin seems to work well by creating a file and storing it on the server, but does nothing when I use send_file, assuming the problem is not in the plugin. But what am I doing wrong with send_file / send_data?
The function itself is as follows:
def user_report
if request.post? unless params[:reports][:userid].blank? @userid=params[:reports][:userid] end if params[:print_state]=='print' report = Spreadsheet::Workbook.new info = report.create_worksheet :name => 'User Information' info.row(1).push 'User ID', @userid @outfile = "Report_for_#{@userid}.xls" require 'stringio' data = StringIO.new '' report.write data send_data data.string, :type=>"application/excel", :disposition=>'attachment', :filename => @outfile end respond_to do |format| format.js { } end end
end
The log file is being read 2010-10-18 14:13:59 INFO - Sending data Report_for_jjohnson.xls but the download does not start in the browser. I will be able to use send_data in this application before, which is confusing.
I use Rails v2.3, Ruby v1.8.7 and Spreadsheet v6.4.1 on the spreadsheet.rubyforge.org page.
ruby-on-rails excel spreadsheet xml-spreadsheet
Chris k
source share