Downloading files not stored in database?

Hello list,

I am trying to add functionality to my application that will allow users to download files that are not stored in a database.

All the examples I have found find the file by ID. which I can not do. Here is my code from the view:

<h1>Browse files for download</h1> <% form_for(:download, :url=>{ :controller=>"download", :action=>"download"}, :html => {:multipart => true}) do |f| %>

<table> <tr> <th>Files</th> </tr> <% @files = Dir.glob("/home/resource_portal/website/public/data/upload/*") %> <% @files.each do |file| %> <tr> <td><%= link_to file, :controller => "download", :action => :download %></td> </tr> <% end %> </table> <% end %> I dont think I need the form_for line at the top, but the fiew is still rendering ok, so have just left it in for now.

If I could get the text that is shown for each link stored in to a variable I could parse it to the upload method in the controller (I think). I would then be able to use this variable in the send_file method, instead of specifying a path manually.

Any ideas on how to do this or better suggestions would be great.

Cheers, Jen.

Below is code from my controller so far. I have commented out the send_file line for now, so I could test the view rendered ok.

class DownloadController < ApplicationController require 'grit' include Grit #First check the user is logged in before_filter :authenticate #First get a list of files from git, so the user can decide what to download. def index download = Download.new render 'download' end def download #Create string for the directory directory = "/home/resource_portal/website/public/data/upload/" #send_file(directory + params[:file], :disposition => :attachment) end

Hello list,

I am trying to add functionality to my application that will allow users to download files that are not stored in a database.

All the examples I have found find the file by ID. which I can not do. Here is my code from the view:

Browse files for download

<% form_for(:download, :url=>{ :controller=>“download”, :action=>“download”}, :html => {:multipart => true}) do |f| %>

<% @files = Dir.glob(“/home/resource_portal/website/public/data/upload/*”) %>

<% @files.each do |file| %>

<% end %>

Files
<%= link_to file, :controller => "download", :action => :download %>

<% end %>

I dont think I need the form_for line at the top, but the fiew is still rendering ok, so have just left it in for now.

Jen, hi. Maybe you have considered against it but one thing is to use CarrierWave or Paperclip. It does save data to a model in the db but the files store in the FS. But even if you dont, I think this should work if the path is valid, as in my case it is just Paperclip providing the file path, then just link to the controller action which provides this:

send_file @billing_run.tu_raw_bill_csv.path

send_file @billing_run.tu_raw_bill_csv.path

Hi David.

All my files are being versioned with GIT, so I don't really want to

use a DB table just for storing IDs if I can help it.

Would it be possible to have the action in the controller farm out

to the web-server to serve the files as long as they are in an authorized directory, then use grit to checkout the downloaded content in GIT?

I still have the problem of telling the server exactly which file to

serve.

The idea is that users can annotate stuff then re-upload it. These

changes will be re-committed, so a history of the file will be stored. This does screw up if a user changes the filename, but will deal with that later!

Thanks in advance for any more help/suggestions,

Jen!
            If I could get the text that is shown for each link

stored in to a variable I could parse it to the upload method in the controller (I think). I would then be able to use this variable in the send_file method, instead of specifying a path manually.

            Any ideas on how to do this or better suggestions would

be great.

            Cheers,

            Jen.



            Below is code from my controller so far. I have

commented out the send_file line for now, so I could test the view rendered ok.

            class DownloadController < ApplicationController

            require 'grit'

            include Grit

            #First check the user is logged in

            before_filter :authenticate

            #First get a list of files from git, so the user can

decide what to download.

            def index

            download = Download.new

            render 'download'

            end

            def download

            #Create string for the directory

            directory = "/home/resource_portal/website/public/data/upload/"

            #send_file(directory + params[:file], :disposition =>

:attachment)

            end

            end




              --

              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@googlegroups.com.

              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](http://groups.google.com/group/rubyonrails-talk?hl=en).
    Jen, hi. Maybe you have considered against it but one thing is

to use CarrierWave or Paperclip. It does save data to a model in the db but the files store in the FS. But even if you dont, I think this should work if the path is valid, as in my case it is just Paperclip providing the file path, then just link to the controller action which provides this:-

send_file @billing_run.tu_raw_bill_csv.path

Hi David.

All my files are being versioned with GIT, so I don't really want to

use a DB table just for storing IDs if I can help it.

But from what you write below, esp that files get changed it sounds to me like you are going to be spending some time writing and polishing code to handle this. If I understand what you are doing correctly, I believe you will spend more time and frustration doing it yourself than letting a gem like paperclip or carrierwave handle. But that said…

Would it be possible to have the action in the controller farm out

to the web-server to serve the files as long as they are in an authorized directory, then use grit to checkout the downloaded content in GIT?

You could do this, but then you would just bypass your controller, right? The link you provide would be to a file on your authroized directory for the user. I have not done this but sounds possible… and then how are you handling checking in and out of git? Sounds like a lot of work… I have done similar but using S3 + paperclip, which to me sounds a lot easier. But again, I dont know your reasons for using git… and you probably have them.

I still have the problem of telling the server exactly which file to

serve.

Someone may correct me, but I believe if you are going to depend on your webserver to do this, you would need to follow how your web server does this combined with the structure of your filesystem.

The idea is that users can annotate stuff then re-upload it. These

changes will be re-committed, so a history of the file will be stored. This does screw up if a user changes the filename, but will deal with that later!

Oh, I see, yes, that might be a reason to use git but I believe you can have paperclip and maybe carrierwave save revisions although this has not be an interest to me yet.

Hi David and list,

After a lot of googling I found an obscure forum post, that enabled

me to write the following code. This code seems to have solved my problem for now, so posting here in case anyone else finds useful.

Thanks for all help with this question,

Jen!

<h1>Browse files for download</h1>

<table>

  <tr>

    <th>Files</th>

</tr>

<% @files =

Dir.glob(“/home/resource_portal/website/public/data/upload/*”) %>

<% @files.each do |file| %>

<tr>

<td><%= link_to file, :action => :download, :file_name

=> file %>

</tr>

<% end %>

</table>

class DownloadController < ApplicationController

require 'grit'

include Grit

#First check the user is logged in

before_filter :authenticate

def download

if !params[:file_name].blank?

send_file "#{params[:file_name]}", :type=>"application/zip"

else

render 'download'

end

end

#end class

end

Will add the git stuff using 'Grit' gem later.