简体   繁体   English

下载使用回形针附加的图像

[英]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:关于发送文件,您可以在此处找到所有信息http://api.rubyonrails.org/classes/ActionController/Streaming.html最重要的削减:

Simple download:简单下载:

send_file '/path/to.zip'

Show a JPEG in the browser:在浏览器中显示 JPEG:

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

Show a 404 page in the browser:在浏览器中显示 404 页面:

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:要使用此选项之一,您必须在 controller 中创建一个新操作,如下所示:

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

Here's a simple solution using the HTML5 download attribute这是使用HTML5下载属性的简单解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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