简体   繁体   中英

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?

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

Here's a simple solution using the HTML5 download attribute

<%= link_to image.name, image.attachment.url, download: image.attachment.original_filename %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM