gpt4 book ai didi

ruby-on-rails - 在 s3 上使用 redirect_to 文件时。如何显示图像或下载文件

转载 作者:行者123 更新时间:2023-12-04 06:28:00 25 4
gpt4 key购买 nike

在 Rails 3 AttachmentsController 中,我有以下内容:

  def show
attachment = Attachment.find_by_id(params[:id])
redirect_to(attachment.authenticated_url())
end

其中 authenticated_url 只是 S3 访问文件的 URL。

问题是该文件总是由浏览器下载。我希望发生的是,如果文件是图像/pdf,浏览器可以渲染,在浏览器中显示文件,并且只下载非浏览器友好的文件。

你以前见过这个吗?关于从哪里开始的任何想法?

谢谢

最佳答案

send_file 也可用于远程 url

file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "attachment")

这里将下载example.pdf。如果你想在浏览器中打开 pdf 使用这个
file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "inline")

关于ruby-on-rails - 在 s3 上使用 redirect_to 文件时。如何显示图像或下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13384441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com