1

Thus far I have successfully set up CarrierWave so that it can upload many files including .pdf, .mp3, .wav, .mp4, and .mov. Awesome that's a pretty good success for me.

What I am trying to do now is to display those Uploads. On that display, I would like two links, one to download the uploaded file and the other to view the uploaded file. Currently, viewing the uploaded files works when I was attempting to create a way for the user to download the file.

Currently, here is the code that allows a user to view/listen/watch the uploaded file:

<% @user.uploads.each do |upload| %>
  <tr>
    <td><%= link_to upload.name, upload.attachment_url.to_s %></td>
    <td class="right_align"><%= upload.to_a_date %></td>
    <td class="right_align"><%#= upload.file_size %></td>
  </tr>
<% end %>

Clicking on the link above allows a user to view the upload. How would I allow a user to download the same content? This gives me the correct path, but when I click on the link it views the file rather than downloads it.

I have also tried to do this in html5 with the following code:

<a href="#{upload.attachment_url.to_s}" download>Download</a>

This doesn't download the correct file.

user2184718
  • 675
  • 1
  • 12
  • 30

1 Answers1

0

Create separate action for this and use Rails send_file method:

def download_file
  upload = Upload.find(params[:id])
  send_file upload.attachment.url
end
NARKOZ
  • 27,203
  • 7
  • 68
  • 90