# How can I open a file via a clickable link

**URL:** <https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442>\
**Category:** Rails\
**Created:** [February 22, 2007, 10:20am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442 "2007-02-22T10:20:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 10:20am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/1 "2007-02-22T10:20:34Z")

</div>

I am new to RoR,  
How can I open a file via a clickable link.  
suppose I have a excel file in a folder named “myfiles” in the public  
directory.  
as example  
I have an excel(named as data.xls) file in “myfiles” directory

Now i want to allow the user to open the file by clicking a link in my  
page.

How can i do that?  
please help me.

Tuhin

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 10:25am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/2 "2007-02-22T10:25:58Z")

</div>

Just make an html link to it, same as you would for any other system.  
-Nathan

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 10:29am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/3 "2007-02-22T10:29:10Z")

</div>

API is your friend.

> **[ActionController::Streaming](https://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091)**
>
> 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.

send\_file

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 10:36am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/4 "2007-02-22T10:36:00Z")

</div>

Either method will work… the advantage of Ben’s method suggestion is  
that  
you can have Ruby code which runs each time the file is downloaded,  
whereas  
with my method, the file is just downloaded directly.  
-N

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 11:11am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/5 "2007-02-22T11:11:23Z")

</div>

Ben wrote:

> API is your friend.  
> [ActionController::Streaming](http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091)  
> send\_file

I put this code block in files\_controller.rb

def send\_file(path, options = {}) #:doc:  
raise MissingFile, “Cannot read file #{path}” unless  
File.file?(path) and File.readable?(path)

```
    options[:length] ||= File.size(path)
    options[:filename] ||= File.basename(path)
    send_file_headers! options

    @performed_render = false

    if options[:stream]
      render :text => Proc.new { |response, output|
        logger.info "Streaming file #{path}" unless logger.nil?
        len = options[:buffer_size] || 4096
        File.open(path, 'rb') do |file|
          if output.respond_to?(:syswrite)
            begin
              while true
                output.syswrite(file.sysread(len))
              end
            rescue EOFError
            end
          else
            while buf = file.read(len)
              output.write(buf)
            end
          end
        end
      }
    else
      logger.info "Sending file #{path}" unless logger.nil?
      File.open(path, 'rb') { |file| render :text => file.read }
    end
  end

```

And add this line in my page  
\<%= link\_to ‘file download’, :action =\> ‘send\_file(download\_filename)’  
%\>  
where download\_filename is a veriable \<%=  
download\_filename=‘/images/myfile.xls’%\>

when I click the link, it display error message as  
“No action responded to send\_file(download\_filename)”  
Any syntax error in my code?  
plz help me as i am new.

Tuhin

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 11:33am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/6 "2007-02-22T11:33:25Z")

</div>

Hold on, no need to copy its source to your controller.

\<%= link\_to ‘Get File’, :action =\> ‘get\_file’, :file =\>  
download\_filename %\>

def get\_file  
send\_file(params[:file])  
end

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 11:44am UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/7 "2007-02-22T11:44:44Z")

</div>

Ben wrote:

> Hold on, no need to copy its source to your controller.
> 
> \<%= link\_to ‘Get File’, :action =\> ‘get\_file’, :file =\>  
> download\_filename %\>
> 
> def get\_file  
> send\_file(params[:file])  
> end

thanks for ur help.

but error is  
“Cannot read file myfile.xls”

why? i can not understand.

Tuhin

---

<div class="post-metadata">

**Author:** ![tuhin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tuhin](https://www.ruby-forum.com/u/tuhin)\
**Post date:** [February 22, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/how-can-i-open-a-file-via-a-clickable-link/86442/8 "2007-02-22T12:50:02Z")

</div>

you need to specify the correct path.  
\<% download\_filename="#{RAILS\_ROOT}/public/files/myfile.xls"%\>
