Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道&x2B;carrierwave S3:强制下载链接_Ruby On Rails_Carrierwave - Fatal编程技术网

Ruby on rails 轨道&x2B;carrierwave S3:强制下载链接

Ruby on rails 轨道&x2B;carrierwave S3:强制下载链接,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,问题: 我想强制link_下载从S3获取的图像和PDF,而不是在浏览器窗口中打开它们 link_to File.basename(asset.attachment.path), asset.attachment_url.to_s 我寻找解决方案,但我发现唯一的方法是在控制器中使用send_file或send_data来处理它,但这些方法对我不起作用。最后,我在Carrierwave sources中偶然发现了解决方案。解决方案: 这是非常有效的方法。使用“响应内容处置”作为url的参数 lin

问题:

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

link_to File.basename(asset.attachment.path), asset.attachment_url.to_s
我寻找解决方案,但我发现唯一的方法是在控制器中使用send_file或send_data来处理它,但这些方法对我不起作用。最后,我在Carrierwave sources中偶然发现了解决方案。

解决方案:

这是非常有效的方法。使用“响应内容处置”作为url的参数

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

在这里找到更多选项:(第185行)

您可以通过添加fog_attributes方法,为特定上传程序的所有文件设置默认值

e、 g


这也适用于未签名的请求

请注意,您可以用File_identifier代替File.basename(..)此解决方案不适用于我。下面的代码似乎忽略了配置并重定向整个页面,而不仅仅是下载文件<代码>链接到attachment.file\u标识符,attachment.file\u url(:query=>{“content disposition”=>“attachment”})。到
# your_uploader.rb
def fog_attributes
  {'Content-Disposition' => "attachment"}
end