Rails: How to download a previously uploaded document? Rails: How to download a previously uploaded document? ruby-on-rails ruby-on-rails

Rails: How to download a previously uploaded document?


I don't use Carrierwave but I guess it's similar to Paperclip.

Therefore you should be able to use something like this

 link_to 'Download file', user.file.url

This is assuming you have a user instantiated object from a Model with a 'file' carrierwave attribute. Replace this with your attribute name.


You can use the send_file method for this. Which could look like:

class MyController  def download_file    @model = MyModel.find(params[:id])    send_file(@model.file.path,          :filename => @model.file.name,          :type => @model.file.content_type,          :disposition => 'attachment',          :url_based_filename => true)  endend

Check the apidock link for more examples.


Or you can put anchor tag:

in your controller:

@doc = "query to get the document name from your database"@docpath = request.base_url+"/uploads/"+@doc

in your view:

<a href="@docpath" download>Download File</a>.