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

In the Rails 3 AttachmentsController, I have the following:

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

If authenticated_url is just the S3 URL to access the file.

The problem is that the file is always loaded by the browser. What I would like to have is if the file is an image / pdf, then that the browser can display, show the file in the browser and only download files that do not support the browser.

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

thanks

+4
source share
3 answers

send_file can also be used for a remote URL.

 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 to open pdf in the browser itself, use this

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

I think you will want to learn the send_file rails method.

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

The: disposition option allows you to decide whether the file will be downloaded or displayed as embedded. I used this in my rails application to allow users to download mp3 files.

Hope this helps.

0
source
 redirect_to @attachment.url 

I also use Paperclip and this image is displayed in the browser. Do you have to do something other than this?

0
source

Source: https://habr.com/ru/post/1446056/


All Articles