Hello,
I am trying to provide a link which, when clicked, will supply a
folder which I''ve previously selected for users to click and open
various files in (some pdf, some .doc, maybe even .odt).
In routes.rb I put:
get ''public'' => ''people#upload''
In application.html.erb I put:
<a href="http://localhost:3000/public" >Read a File</a>
And in people_controller.rb requesting a specific file works to
open the correct download window:
def upload
send_file("C:\\ -- a specific folder and file --", :disposition
=>
''attachment'' , :type =>
''application/pdf'')
end
But since I don''t want to preselect a file for them, from this
preselected folder, how to I just show the folder and let the user
double-click which one they want to open in another window?
Thanks,
Barney
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
One way: 1) Get the filenames: Dir.foreach or Dir.glob. 2) Create a view that provides a list of links, one for each file. 3) The links connect to another action, which sends the file. Why is your action named ''upload''? How about ''download''? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks, I''ll give those steps a try.
The name is leftover from earlier work and should be changed, as
you''ve pointed out.
Barney
On Aug 17, 7:52 pm, 7stud --
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> One way:
>
> 1) Get the filenames: Dir.foreach or Dir.glob.
> 2) Create a view that provides a list of links, one for each file.
> 3) The links connect to another action, which sends the file.
>
> Why is your action named ''upload''? How about
''download''?
>
> --
> Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Could you provide a code example of the connection between the links
and an action that sends the file? I now have a list of links on a
page but clicking them gives:
"Firefox doesn''t know how to open this address, because the
protocol
(c) isn''t associated with any program."
Thanks,
Barney
On Aug 17, 8:43 pm, Barney
<bsper...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Thanks, I''ll give those steps a try.
> The name is leftover from earlier work and should be changed, as
> you''ve pointed out.
> Barney
>
> On Aug 17, 7:52 pm, 7stud --
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:
>
>
>
>
>
>
>
> > One way:
>
> > 1) Get thefilenames: Dir.foreach or Dir.glob.
> > 2) Create a view that provides a list oflinks, one for each file.
> > 3) Thelinksconnect to another action, which sends the file.
>
> > Why is your action named ''upload''? How about
''download''?
>
> > --
> > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Okay. But I am a beginner too, so I don''t know if this is the best
way.
I''m assuming the path to your folder is:
/public/files_to_read
class PagesController < ApplicationController
def home
@title = "Home"
end
def get_files
dir_path = ''public/files_to_read''
Dir.chdir(dir_path) do
@fnames = Dir.glob("*")
end
end
def download
dir_path = ''public/files_to_read''
fname = params[:fname]
Dir.chdir(dir_path) do
allowed_fnames = Dir.glob("*")
if allowed_fnames.include?(fname)
send_file("#{RAILS_ROOT}/#{dir_path}/#{fname}",
:filename => fname)
else
@title = ''Home''
render ''home''
end
end
end
end
==
Test2App::Application.routes.draw do
root :to => "pages#home"
get ''pages/get_files''
get ''pages/download''
==
<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
<%= link_to "Read a file", {:controller =>
''pages'', :action =>
''get_files''} %>
==
<h1>Pages#get_files</h1>
<div>Find me in app/views/pages/get_files.html.erb</div>
<h3>Click the file you want to download:</h3>
<% @fnames.each do |fname| %>
<div><%= link_to fname, :controller => ''pages'',
:action => ''download'',
:fname => fname %></div>
<% end %>
==
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
==
http://localhost:3000 => home.html.erb
click on Read file link => get_file.html.erb
click on a filename link => computer downloads the file
The reason for the code:
if allowed_names.include?( )
is to prevent a hacker from going to the page of links, and then instead
of clicking on a link, entering:
http://localhost:3000/pages/download?fname=/path/to/secrets.txt
If you don''t check the fname that the server receives, a hacker can
download any file they want.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks 7stud! Your code works and I integrated it with my version.
The only changes I made were to redo RAILS_ROOT to use: dir_path
Rails.root.join("public","resumes")
and to make the send_file more "Windowsy" in this way:
send_file("#{dir_path}\\#{fname}", :filename => fname)
I appreciate your taking the time!
Barney
On Aug 18, 10:49 pm, 7stud --
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> Okay. But I am a beginner too, so I don''t know if this is the
best way.
> I''m assuming the path to yourfolderis:
>
> /public/files_to_read
>
> class PagesController < ApplicationController
> def home
> @title = "Home"
> end
>
> def get_files
> dir_path = ''public/files_to_read''
>
> Dir.chdir(dir_path) do
> @fnames = Dir.glob("*")
> end
>
> end
>
> def download
> dir_path = ''public/files_to_read''
> fname = params[:fname]
>
> Dir.chdir(dir_path) do
> allowed_fnames = Dir.glob("*")
>
> if allowed_fnames.include?(fname)
> send_file("#{RAILS_ROOT}/#{dir_path}/#{fname}",
> :filename => fname)
> else
> @title = ''Home''
> render ''home''
> end
>
> end
>
> end
>
> end
>
> ==>
> Test2App::Application.routes.draw do
> root :to => "pages#home"
>
> get ''pages/get_files''
> get ''pages/download''
>
> ==>
> <h1>Pages#home</h1>
> <p>Find me in app/views/pages/home.html.erb</p>
>
> <%= link_to "Read afile", {:controller =>
''pages'', :action =>
> ''get_files''} %>
>
> ==>
> <h1>Pages#get_files</h1>
> <div>Find me in app/views/pages/get_files.html.erb</div>
>
> <h3>Click thefileyou want to download:</h3>
>
> <% @fnames.each do |fname| %>
> <div><%= link_to fname, :controller =>
''pages'', :action => ''download'',
> :fname => fname %></div>
> <% end %>
>
> ==>
> <!DOCTYPE html>
> <html>
> <head>
> <title><%= @title %></title>
> <%= csrf_meta_tag %>
> </head>
> <body>
>
> <%= yield %>
>
> </body>
> </html>
>
> ==>
> http://localhost:3000=> home.html.erb
> click on Readfilelink => get_file.html.erb
> click on a filename link => computer downloads thefile
>
> The reason for the code:
>
> if allowed_names.include?( )
>
> is to prevent a hacker from going to the page of links, and then instead
> of clicking on a link, entering:
>
> http://localhost:3000/pages/download?fname=/path/to/secrets.txt
>
> If you don''t check the fname that the server receives, a hacker
can
> download anyfilethey want.
>
> --
> Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Never use backslashes in paths. ruby and other modern languages can handle forward slashes for path separators no matter what os the program is running on. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Barney wrote in post #1017759:> Thanks 7stud! Your code works and I integrated it with my version. > The only changes I made were to redo RAILS_ROOT to use: dir_path > Rails.root.join("public","resumes") >>>As of Rails 3.0.7 RAILS_ROOT and RAILS_ENV are deprecated. >>Rails.root and Rails.env are preferred.Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.