0

I currently have this code in my view:

= link_to "Download", upload.upload.expiring_url, class: "btn btn-sm btn-success pull-right margin-right"

Right now, this simply links to the file and opens it within the browser. How can I force the file to download when the link is clicked?

Joel Brewer
  • 1,622
  • 19
  • 30

2 Answers2

2

I ended up following the advice left on another answer (Allowing User to Download File from S3 Storage)

I added a download_url method to my uploads controller:

def download_url(style_name=:original)
  s3 = AWS::S3.new
  @bucket ||= s3.buckets[upload.bucket_name]
  @bucket.objects[upload.s3_object(style_name).key].url_for(:read,
    :secure => true,
    :expires => 24*3600,
    :response_content_disposition => "attachment; filename='#{upload_file_name}'").to_s
end

And then in my view I've got:

= link_to "Download", upload.download_url
Community
  • 1
  • 1
Joel Brewer
  • 1,622
  • 19
  • 30
0

The files in S3 need to have following headers set.

Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream

Either check paperclip options to see if it can do it on file upload. Or you can do it manually in s3 console, one file at a time.

San
  • 1,954
  • 1
  • 14
  • 18