To view folders of the file system from browser in RoR
I would like to have a link in my browse开发者_StackOverflowr which when clicked opens a folder in the file system. I don't want it to get downloaded but only to be viewed. Is this possible in Ruby? What command can be used? Help!
Yes, this is possible. You can use the Dir
class to get the contents of a directory (emphasis mine):
Objects of class
Dir
are directory streams representing directories in the underlying file system. They provide a variety of ways to list directories and their contents. See alsoFile
.
and then the usual Rails mechanisms to display that data to the user.
If you mean on the server and not on the user's computer:
Dir.foreach("/path/to/directory") { |file|
puts file
}
You can do the same thing with Dir#glob
too, except it will also take a wildcard pattern:
Dir.glob("/path/to/directory/*") { |file|
puts file
}
Obviously puts
won't cut it in Rails, but you get the idea.
Note that you can simply insert a link to your local file system. For example,
<a href="/home/user/foo">open foo</a>
gives a link to /home/user/foo
on your computer.
I hope this will help you :-
https://richonrails.com/articles/basic-file-and-directory-operations-in-ruby
http://code.tutsplus.com/tutorials/ruby-for-newbies-working-with-directories-and-files--net-18810
精彩评论