Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file on click - Ruby on Rails

My application is using Rails 2 backend, Heroku for hosting, Paperclip for file uploads, and Amazon S3 for file storage.

Right now users can upload files with paperclip + s3 - this works flawlessly. After upload, an icon appears on their dashboard, linked to the file location (in s3 bucket). When the icon is clicked, the browser opens the file in a new window (for most file types - PDF, MP3, img, etc). Instead of opening, I want the file to be automatically downloaded when the user clicks the file's icon (like Gmail attachments). The solution should be able to work for any file type and cross-browser.

Is there a helper to do this in rails, or is javascript needed? I'm really stuck on this one so anything to point me in the right direction would be greatly appreciated. Thanks!

like image 759
aguynamedloren Avatar asked Jan 27 '11 00:01

aguynamedloren


1 Answers

Please try the following:

class Test < ActiveRecord::Base

  has_attached_file :testfile, 
    :storage => :s3,
    # All your S3 config
    :s3_headers => {"Content-Disposition" => "attachment"}

end

This should tell the Paperclip Gem to set the "Content-Disposition" header to the value "attachment" for newly uploaded files.

Note that you have to manually edit the already uploaded file, e.g. with Cyberduck or another FTP Client.

like image 183
Michael Schäfermeyer Avatar answered Sep 18 '22 10:09

Michael Schäfermeyer