Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Image attached using Paperclip

Is there a way to make the users download the images attached using paperclip?

I just made a link like this:

link_to 'imagename', image.attachment.url

But this makes the browser open the image. I want the browser to ask the user to save the image. Is there a way to do that?

like image 866
thitemple Avatar asked Jan 19 '26 20:01

thitemple


1 Answers

When it comes to sending a file you can find all information in here http://api.rubyonrails.org/classes/ActionController/Streaming.html There are most important cuts:

Simple download:

send_file '/path/to.zip'

Show a JPEG in the browser:

send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'

Show a 404 page in the browser:

send_file '/path/to/404.html', :type => 'text/html; charset=utf-8', :status => 404

to use one of this option you have to create a new action in controller like this:

class SomeController < ApplicationController
  def download_file
    send_file image.attachment.path
  end
end
like image 170
Ireneusz Skrobis Avatar answered Jan 21 '26 10:01

Ireneusz Skrobis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!