Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip Force Download

Hope anyone can help on this!

Making use of Paperclip to upload Files to an Application, so two things that i need to cover.

1) What is the best way to link to the File for Download, I am currently linking to the public folder but it is not the best option as it shows the URL which i dont want. Thinking maybe a button_to call be not sure if this is build into Paperclip already. Options.

2) When above is done then the Browser needs to force to download not just open the file, or at least give the user the standard firefox option open or save.

Please Assist Thanks a Mil!!!

like image 857
Clay Avatar asked Jan 18 '13 19:01

Clay


1 Answers

I'll answer question Nº2.
Let's say your model is called gallery.
In your gallery controller you'll add a download method:

def download
  @gallery= Gallery.find(params[:gallery_id])

  send_file @gallery.gallery_picture.path,
              :filename => @gallery.gallery_picture_file_name,
              :type => @gallery.gallery_picture_content_type,
              :disposition => 'attachment'
end

Now from your routes you'll invoke the root to this method:

match 'gallery/:id' => 'gallery#download', :as => :download

In your views:

- @galleries.each do |gallery|     
  = link_to 'Download Picture', download_path(gallery.id)

I'm away from home and I can not test this code, but you can visit those questions I did with the same question as yours. Let me know if it solves your problem.

Rails send_file multiple styles from Paperclip, how can I avoid code repetition?
Rails 3.0 associated model with download 'save to' image

like image 96
Ricardo Castañeda Avatar answered Sep 23 '22 17:09

Ricardo Castañeda