在s3上使用redirect_to文件时.如何显示图像或下载文件

Rac*_*Roy 4 ruby-on-rails ruby-on-rails-3

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

  def show
    attachment = Attachment.find_by_id(params[:id])
    redirect_to(attachment.authenticated_url())
  end
Run Code Online (Sandbox Code Playgroud)

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

问题是该文件总是由浏览器下载.我想要发生的是,如果文件是image/pdf,浏览器可以渲染,在浏览器中显示文件,只下载非浏览器友好文件.

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

谢谢

adv*_*sad 5

send_file也可用于远程URL

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

这里将下载example.pdf.如果你想在浏览器中打开pdf,请使用它

file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "inline")
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这实际上是先将文件下载到 rails 服务器吗? (3认同)
  • 好的,这样就行不通了,我不能让应用服务器完成所有下载工作。那是阻塞。试图找到一种方法让 s3/paperclip 内联呈现 (2认同)