我正在使用Paperclip + S3 - 我如何强制下载?

时间:2014-05-16 22:56:41

标签: ruby-on-rails ruby-on-rails-4 amazon-s3 paperclip

我目前在我看来有这段代码:

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

现在,这只是链接到文件并在浏览器中打开它。如何在单击链接时强制下载文件?

2 个答案:

答案 0 :(得分:2)

我最后听到了另一个答案(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

答案 1 :(得分:0)

S3中的文件需要设置以下标题。

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

检查回形针选项以查看它是否可以在文件上传时执行此操作。或者你可以在s3控制台中手动完成,一次一个文件。