# Force the download

**URL:** <https://www.ruby-forum.com/t/force-the-download/119396>\
**Category:** Rails\
**Created:** [November 6, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/force-the-download/119396 "2007-11-06T18:41:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [November 6, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/force-the-download/119396/1 "2007-11-06T18:41:07Z")

</div>

Hello,

I wonder if you could tell me how to make the navigator force the  
download.

In PHP it works like this:

$downloadfile=“fichero.gpx”;  
header(“Content-disposition: attachment; filename=$downloadfile”);  
header(“Content-Type: application/force-download”);  
header(“Content-Transfer-Encoding: binary”);  
header("Content-Length: ".strlen($myGPXCode));  
header(“Pragma: no-cache”);  
header(“Expires: 0”);  
echo $myGPXCode;

What would be a similar code to the above for Ruby on Rails?

Thank you.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [November 6, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/force-the-download/119396/2 "2007-11-06T18:51:59Z")

</div>

On 11/6/07, David B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $downloadfile=“fichero.gpx”;  
> header(“Content-disposition: attachment; filename=$downloadfile”);  
> header(“Content-Type: application/force-download”);  
> header(“Content-Transfer-Encoding: binary”);  
> header("Content-Length: ".strlen($myGPXCode));  
> header(“Pragma: no-cache”);  
> header(“Expires: 0”);  
> echo $myGPXCode;
> 
> What would be a similar code to the above for Ruby on Rails?

send\_data myGPXCode, :filename =\> ‘fichero.gpx’

You can’t actually “force” the browser to do anything, but this gives  
the proper “suggestions” to the browser to trigger the “save as”  
dialog…

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [November 6, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/force-the-download/119396/3 "2007-11-06T18:53:45Z")

</div>

You are looking for send\_file:

> **[ActionController::Streaming](https://api.rubyonrails.org/classes/ActionController/Streaming.html#M000092)**
>
> Allows views to be streamed back to the client as they are rendered. By default, Rails renders views by first rendering the template and then the layout.

-Bill

Bob S. wrote:

> > What would be a similar code to the above for Ruby on Rails?
> 
> send\_data myGPXCode, :filename =\> ‘fichero.gpx’
> 
> You can’t actually “force” the browser to do anything, but this gives  
> the proper “suggestions” to the browser to trigger the “save as”  
> dialog…
> 
> >

–  
Sincerely,

William P.
