# Can't download files with send\_data or send\_file

**URL:** <https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278>\
**Category:** Rails\
**Created:** [June 8, 2007, 1:13am UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278 "2007-06-08T01:13:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 8, 2007, 1:13am UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/1 "2007-06-08T01:13:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 8, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/2 "2007-06-08T17:33:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/3 "2007-06-09T20:11:29Z")

</div>

Thanks! That fixed things right up.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 19, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/4 "2007-06-19T19:44:25Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 10, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/5 "2008-09-10T18:15:35Z")

</div>

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](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 6, 2009, 11:25am UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/6 "2009-02-06T11:25:22Z")

</div>

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 %\>
> 
> \<%= submit\_tag 'Export To Excel' %\> \<%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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 19, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/cant-download-files-with-send-data-or-send-file/106278/7 "2007-06-19T19:11:21Z")

</div>

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 %\>

 \<%= submit\_tag 'Export To Excel' %\> \<%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.
