[英]Download Image attached using Paperclip
有沒有辦法讓用戶下載使用回形針附加的圖像?
我剛剛做了一個這樣的鏈接:
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
這是使用HTML5下載屬性的簡單解決方案
<%= link_to image.name, image.attachment.url, download: image.attachment.original_filename %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.