Skip to main content
 首页 » 编程设计

ruby-on-rails之下载使用 Paperclip 附加的图像

2025年12月25日25cmt

有没有办法让用户下载使用回形针附加的图像?

我刚刚做了一个这样的链接:

link_to 'imagename', image.attachment.url 

但这会使浏览器打开图像。我希望浏览器要求用户保存图像。有办法吗?

请您参考如下方法:

关于发送文件,您可以在此处找到所有信息 http://api.rubyonrails.org/classes/ActionController/Streaming.html有最重要的削减:

简单下载:

send_file '/path/to.zip' 

在浏览器中显示 JPEG:

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

在浏览器中显示一个 404 页面:

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

要使用此选项之一,您必须像这样在 Controller 中创建一个新操作:

class SomeController < ApplicationController 
  def download_file 
    send_file image.attachment.path 
  end 
end