# How to download any file using send\_file() method

**URL:** <https://www.ruby-forum.com/t/how-to-download-any-file-using-send-file-method/112896>\
**Category:** Rails\
**Created:** [August 24, 2007, 7:24am UTC](https://www.ruby-forum.com/t/how-to-download-any-file-using-send-file-method/112896 "2007-08-24T07:24:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [August 24, 2007, 7:24am UTC](https://www.ruby-forum.com/t/how-to-download-any-file-using-send-file-method/112896/1 "2007-08-24T07:24:06Z")

</div>

HI,

I am directory listing and in that listing I have certain files and sub  
directories, now when I click on the files it should download that  
particular file but I am not able to accomplish it, and I have got to  
use send\_file() method.

In the send file Code shown below I have made use of the address of one  
of the files directly so when I click the link of any file it downloads  
it.

THE CODE  
…

def listing

```
    if (params[:dir] !=nil)
       @path = params[:path]+"/"+params[:dir]
    else
       @path ="D:/hr"
    end

       @f=File.ftype(@path)
          if @f=="file"
             redirect_to :action=>'download_files'
           else
           @dirEntries = Dir.entries(@path)
           @dirEntries.reject! { |filename| filename.starts_with?

```

‘.’ }  
end  
end

```
      def download_files

            send_file('D:/hr/xyz.doc',
                       :filename => params[:filename] ,
                       :type =>

```

‘application/octet-stream’,  
:disposition =\> ‘attachment’,  
:streaming =\> ‘true’,  
:buffer\_size =\> ‘4096’)  
end
