开发者

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 brows开发者_运维技巧er 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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜