5

希望任何人都可以在这方面提供帮助!

利用 Paperclip 将文件上传到应用程序,所以我需要介绍两件事。

1) 链接到下载文件的最佳方式是什么,我目前正在链接到公用文件夹,但这不是最佳选择,因为它显示了我不想要的 URL。想也许一个 button_to 调用不确定这是否已经内置到 Paperclip 中。选项。

2)完成上述操作后,浏览器需要强制下载,而不仅仅是打开文件,或者至少为用户提供标准的 firefox 选项打开或保存。

请协助谢谢米尔!!!

4

2 回答 2

13

I'll answer question Nº2.
Let's say your model is called gallery.
In your gallery controller you'll add a download method:

def download
  @gallery= Gallery.find(params[:gallery_id])

  send_file @gallery.gallery_picture.path,
              :filename => @gallery.gallery_picture_file_name,
              :type => @gallery.gallery_picture_content_type,
              :disposition => 'attachment'
end

Now from your routes you'll invoke the root to this method:

match 'gallery/:id' => 'gallery#download', :as => :download

In your views:

- @galleries.each do |gallery|     
  = link_to 'Download Picture', download_path(gallery.id)

I'm away from home and I can not test this code, but you can visit those questions I did with the same question as yours. Let me know if it solves your problem.

Rails send_file multiple styles from Paperclip, how can I avoid code repetition?
Rails 3.0 associated model with download 'save to' image

于 2013-01-18T21:29:35.290 回答
0

在 Rails 中,send_file用户设置的参数是一个安全漏洞。更多信息

在 rails 4 你不能只写match,你必须在最后添加, via: [:get, :post]更多信息

于 2014-04-01T06:17:38.427 回答