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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-22 09:24 UTC
Re: How can I open a file via a clickable link.
Just make an html link to it, same as you would for any other system. -Nathan On 22/02/07, tuhin <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
API is your friend. http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091 send_file -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-22 09:35 UTC
Re: How can I open a file via a clickable link.
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 On 22/02/07, Ben <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > API is your friend. > http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091 > send_file > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben wrote:> API is your friend. > http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091 > send_fileI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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]) > endthanks for ur help. but error is "Cannot read file myfile.xls" why? i can not understand. Tuhin -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you need to specify the correct path. <% download_filename="#{RAILS_ROOT}/public/files/myfile.xls"%> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---