开发者

Download file on click - Ruby on Rails

开发者 https://www.devze.com 2023-02-07 10:55 出处:网络
My application is using Rails 2 backend, Heroku for hosting, Paperclip for file uploads, and Amazon S3 for file storage.

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!


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.


When you transfer the file, you need to set a Content-Disposition header with a value of attachment; filename=yourfilename.pdf. If it's transfered directly from S3, you'll need to tell S3 to set the Content-Disposition headers as well. Possibly also Content-Type.

Note that if you tell S3 to associate a Content-Disposition header, it will always transmit this header.

FWIW, here's Amazon's documentation on doing a PUT for an Object: http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPUT.html

0

精彩评论

暂无评论...
验证码 换一张
取 消