Rachel D Roy
Rachel D Roy

Reputation: 1035

When using redirect_to files on s3. How to either display an image or download a file

In Rails 3 AttachmentsController, I have the following:

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

Where authenticated_url is simply a URL to S3 to access the file.

The problem is that the file is always downloaded by the browser. What I would like to have happen is if the file is an image/pdf, something the browser can render, show the file in the browser and only download non-browser friendly files.

Have you seen this before? Any ideas on where to start?

Thanks

Upvotes: 4

Views: 4670

Answers (3)

advishnuprasad
advishnuprasad

Reputation: 341

send_file can be used for remote url as well

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

Here example.pdf will be downloaded. If you want open pdf in browser itself use this

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

Upvotes: 5

Sean Xiao
Sean Xiao

Reputation: 616

redirect_to @attachment.url

I'm using Paperclip as well, and this display pictures inside the browser. Do you have to do something different than this?

Upvotes: 0

user1096557
user1096557

Reputation:

I think you'll want to look into the rails send_file method.

1) http://apidock.com/rails/ActionController/DataStreaming/send_file

The :disposition option lets you decide whether a file will be downloaded or displayed inline. I used this in my rails app to let users download mp3 files.

Hope this helps.

Upvotes: -1

Related Questions