Can't download files with send_data or send_file

I have a form_remote_tag that calls a method to print out a csv file for download. If I tell the form_remote_tag to update a div the data comes up in that div, but otherwise I can't get the browser to acknowledge the sent file.

From view:

   <%= form_remote_tag :url => {:action => "csv_dump" }%>    <%= submit_tag "Download Complete List" ,:name => "csv"%>    <%= end_form_tag %>

From controller:

  def csv_dump       @assignments = Assignment.find(:all)       report = StringIO.new       CSV::Writer.generate(report, ',') do |csv|         csv << ['ID', 'ExpoCode','Project', 'CurrentStatus','AssignedTo','DataContact', 'Action','Parameter', 'Priority', 'Deadline','LastChanged', 'Notes','History']         @assignments.each do |task|           csv << [task.id, task.ExpoCode, task.project, task.current_status, task.cchdo_contact, task.data_contact, task.action, task.parameter, task.priority, task.deadline, task.changed, task.notes, task.history]         end       end       report.rewind       send_data(report.read,:type => 'text/csv; charset=iso-8859-1; header=present',:filename => 'report.csv',:disposition => 'attachment')       #send_file("/Users/Shared/cchdo_watershed/public/whp_atlas/ pacific_index.html",:type => 'text',:stream => "false",:disposition => 'attachment')   end

fieldsjustin@gmail.com wrote:

I have a form_remote_tag that calls a method to print out a csv file for download. If I tell the form_remote_tag to update a div the data comes up in that div, but otherwise I can't get the browser to acknowledge the sent file.

From view:

   <%= form_remote_tag :url => {:action => "csv_dump" }%>    <%= submit_tag "Download Complete List" ,:name => "csv"%>    <%= end_form_tag %>

Make the form a non-AJAX form, use submit_to_remote to trigger the AJAX call to update the div, and use an ordinary submit tag to trigger the CSV send.

Thanks! That fixed things right up.

I tried the same in my code. But always the browser is getting only the action name itself as the filename and it says it cannot find the file. Here is the code snippets from my application.

Controller code :

@filepath = "C:/Rails application/smartime/public/excel/" @filename = "report8.xls"     send_file(@filepath + @filename,       :disposition => 'attachment',       :encoding => 'utf8',       :type => 'application/octet-stream')

View code: <% form_tag({ :action => :exportToExcel })do %>   <td width="35%" height="25%" align="left">     <%= submit_tag 'Export To Excel' %>   </td> <%end%>

When I click on the "Export to excel button", the file is created in the server. The browser shows the file download window. But it shows the file name as "exportToExcel". When I click on save it shows error message "Internet explorer cannot download exportToExcelfrom localhost". I am running the server in localhost.

What is missing in the code I am using?

Thanks in advance.

fieldsjustin@gmail.com wrote:

I tried the same in my code. But always the browser is getting only the action name itself as the filename and it says it cannot find the file. Here is the code snippets from my application.

Controller code :

@filepath = "C:/Rails application/smartime/public/excel/" @filename = "report8.xls"     send_file(@filepath + @filename,       :disposition => 'attachment',       :encoding => 'utf8',       :type => 'application/octet-stream',

        :filename => @filename

)

View code: <% form_tag({ :action => :exportToExcel })do %>   <td width="35%" height="25%" align="left">     <%= submit_tag 'Export To Excel' %>   </td> <%end%>

When I click on the "Export to excel button", the file is created in the server. The browser shows the file download window. But it shows the file name as "exportToExcel". When I click on save it shows error message "Internet explorer cannot download exportToExcelfrom localhost". I am running the server in localhost.

What is missing in the code I am using?

Thanks in advance.

Thanks! That fixed things right up.

I have a form_remote_tag that calls a method to print out a csv file for download. If I tell the form_remote_tag to update a div the data comes up in that div, but otherwise I can't get the browser to acknowledge the sent file.

From view:

   <%= form_remote_tag :url => {:action => "csv_dump" }%>    <%= submit_tag "Download Complete List" ,:name => "csv"%>    <%= end_form_tag %>

Make the form a non-AJAX form, use submit_to_remote to trigger the AJAX call to update the div, and use an ordinary submit tag to trigger the CSV send.

-- We develop, watch us RoR, in numbers too big to ignore.

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Hello,

I have a similar problem and I am using select helper alongwith observe_field. Any clues are greatly appreciated.

View:

<%= select('temp_data','choose', ["Spreadsheet","Flat Spreadsheet","Pipe-delimited","Flat Pipe-delimited"], {:selected => nil},{:id => "report_download_select"}) %>

<%= observe_field 'report_download_select', :url => {:action => 'generate_report', :id => @dataset}, :on => 'focus', :with => "'report_download_select=' + $F('report_download_select')", :update => {} %>

Controller:

... ... send_data(output.read, :type => content_type, :filename => filename, :encoding => 'utf8')

Rob Biedenharn wrote:

for filename specify your filename directly with filepath for e.g @filepath = "C:/Rails application/smartime/public/excel/"+"report8.xls" and then pass this variable for filename

It might work

Siva wrote: