【问题标题】:I'm using Paperclip + S3 -- How can I force a download?我正在使用 Paperclip + S3 -- 如何强制下载?
【发布时间】:2014-05-21 22:59:37
【问题描述】:
我目前有这段代码:
= link_to "Download", upload.upload.expiring_url, class: "btn btn-sm btn-success pull-right margin-right"
现在,这只是链接到文件并在浏览器中打开它。点击链接时如何强制下载文件?
【问题讨论】:
标签:
ruby-on-rails
ruby-on-rails-4
amazon-s3
paperclip
【解决方案1】:
我最终遵循了另一个答案 (Allowing User to Download File from S3 Storage) 上的建议
我在上传控制器中添加了一个 download_url 方法:
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
然后在我看来,我有:
= link_to "Download", upload.download_url
【解决方案2】:
S3 中的文件需要设置以下标头。
Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream
要么检查回形针选项,看看它是否可以在文件上传时做到这一点。或者您可以在 s3 控制台中手动执行,一次一个文件。