rails + carrierwave S3: force link_to to download

carrierwave, ruby-on-rails

Solution

Solution:

This is what works super well. Use 'response-content-disposition' as a parameter to url

link_to File.basename(asset.attachment.path), asset.attachment_url(:query => {"response-content-disposition" => "attachment"}).to_s

Find more options here: https://github.com/carrierwaveuploader/carrierwave/blob/5aec4725a94fca2c161e347f02b930844d6be303/lib/carrierwave/uploader/versions.rb (line 185)

Problem

Question: I would like to force link_to to download images and pdfs fetched from S3 instead of opening them in the browser window. ``` link_to File.basename(asset.attachment.path), asset.attachment_url.to_s ``` I looked for solutions but the only ones I found are to handle it in the controller using send_file or send_data but these did not work for me. Finally I stumbled upon the solution in Carrierwave sources.

Original source