Forum: Ruby on Rails Can't download files with send_data or send_file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-06-08 03:13
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2007-06-08 19:33
(Received via mailing list)
removed_email_address@domain.invalid 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.

--
We develop, watch us RoR, in numbers too big to ignore.
(Guest)
on 2007-06-09 22:11
(Received via mailing list)
Thanks!  That fixed things right up.
Siva (Guest)
on 2007-06-19 21:11
(Received via mailing list)
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.
Rob B. (Guest)
on 2007-06-19 21:44
(Received via mailing list)
On Jun 19, 2007, at 1:10 PM, Siva wrote:
>       :encoding => 'utf8',
>       :type => 'application/octet-stream',

        :filename => @filename

> the server. The browser shows the file download window. But it shows
>>
>>>>    <%= 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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Aditya A. (Guest)
on 2008-09-10 20:15
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 B. wrote:
> On Jun 19, 2007, at 1:10 PM, Siva wrote:
>>       :encoding => 'utf8',
>>       :type => 'application/octet-stream',
>
>         :filename => @filename
>
>> the server. The browser shows the file download window. But it shows
>>>
>>>>>    <%= 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 B.    http://agileconsultingllc.com
> removed_email_address@domain.invalid
Sarika P. (Guest)
on 2009-02-06 12:25
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:
> 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.
This topic is locked and can not be replied to.