How can I open a file via a clickable link

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

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

API is your friend.
http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091
send_file

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

Ben wrote:

API is your friend.
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

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

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

you need to specify the correct path.
<% download_filename="#{RAILS_ROOT}/public/files/myfile.xls"%>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs