6

问题:

我想强制 link_to 下载从 S3 获取的图像和 pdf,而不是在浏览器窗口中打开它们。

link_to File.basename(asset.attachment.path), asset.attachment_url.to_s

我寻找解决方案,但我发现的唯一解决方案是使用 send_file 或 send_data 在控制器中处理它,但这些对我不起作用。最后,我偶然发现了 Carrierwave 源中的解决方案。

4

2 回答 2

9

解决方案:

这是非常有效的。使用 'response-content-disposition' 作为 url 的参数

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

在此处查找更多选项:https ://github.com/carrierwaveuploader/carrierwave/blob/5aec4725a94fca2c161e347f02b930844d6be303/lib/carrierwave/uploader/versions.rb (第185行)

于 2013-07-13T15:26:06.007 回答
7

您可以通过添加fog_attributes 方法为特定上传者的所有文件默认此设置。

例如

# your_uploader.rb
def fog_attributes
  {'Content-Disposition' => "attachment"}
end

这也适用于未签名的请求!

于 2014-09-08T13:33:51.793 回答