3

I need to export an XML file. I am able to save the files in the public folder but how do I list the files in this directory. Further, how do I render links such that when clicked by a client browser according file is downloaded.

rudolph9
  • 8,021
  • 9
  • 50
  • 80
user sflskjdf
  • 504
  • 6
  • 10

2 Answers2

3

You can get an array of all file in your public directory using Dir.entries("public"). From there you would simply need to loop through and link to the according name at the root of your application.

So in an ERB file you could use:

<ul>
<% Dir.entries("public")[2..-1].each do |file_name| #[2..-1] as the first two will be ".", ".." %>
  <li><%= link_to file_name, "/#{URI.escape(file_name)}" %></li>
<% end %>
</ul>
rudolph9
  • 8,021
  • 9
  • 50
  • 80
0

You can list the content of public directory from you controller action like this Built in way to list directories in a directory in ruby

and then use this file download link in rails to create links and download the file.

Hope this will help.

Community
  • 1
  • 1
techvineet
  • 5,041
  • 2
  • 30
  • 28