# How can i add download functionality

**URL:** <https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647>\
**Category:** Rails\
**Created:** [May 26, 2008, 11:13am UTC](https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647 "2008-05-26T11:13:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djdon11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8edcca/32.png) [@djdon11](https://www.ruby-forum.com/u/djdon11)\
**Post date:** [May 26, 2008, 11:13am UTC](https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647/1 "2008-05-26T11:13:48Z")

</div>

Hello friends

i have to generate a text file when clicked on a button and when this  
file got generated than i have to show a download dialog box so that  
user can download this text file on his computer, can any body gives any  
snippet of code or any idea how can i add this download functionality.

Thanks

---

<div class="post-metadata">

**Author:** ![djdon11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8edcca/32.png) [@djdon11](https://www.ruby-forum.com/u/djdon11)\
**Post date:** [May 26, 2008, 11:36am UTC](https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647/2 "2008-05-26T11:36:06Z")

</div>

your download dialog should simply link to an action that uses send\_file  
to send the text file:

send\_file(File.join(FILE\_PATH, file\_name), :filename =\> ‘something.txt’,  
:disposition =\> ‘attachment’, :type =\> ‘text/plain’

---

<div class="post-metadata">

**Author:** ![djdon11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8edcca/32.png) [@djdon11](https://www.ruby-forum.com/u/djdon11)\
**Post date:** [May 26, 2008, 11:51am UTC](https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647/3 "2008-05-26T11:51:39Z")

</div>

Thorsten M. wrote:

> your download dialog should simply link to an action that uses send\_file  
> to send the text file:
> 
> send\_file(File.join(FILE\_PATH, file\_name), :filename =\> ‘something.txt’,  
> :disposition =\> ‘attachment’, :type =\> ‘text/plain’

Hello friend,  
Thanks for your help but it is not working for me , instead of the  
download dialog box its just showing me the content of the text file.  
can u give me any idea why this is happening… ?

Thanks

---

<div class="post-metadata">

**Author:** ![djdon11](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8edcca/32.png) [@djdon11](https://www.ruby-forum.com/u/djdon11)\
**Post date:** [September 12, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/how-can-i-add-download-functionality/138647/4 "2008-09-12T20:34:57Z")

</div>

> Hello friend,  
> Thanks for your help but it is not working for me , instead of thedownloaddialogbox its just showing me the content of the text file.  
> can u give me any idea why this is happening… ?

You can also use send\_data(). Instead of generating the output and  
then writing it to a file and then attempting to send that file, you  
can just generate the output and send it, skipping the file part:

data = “A String you generated somehow”

send\_data( data, :filename =\> ‘filename.txt’ )
