Paperclip Force下载

Cla*_*lay 4 ruby-on-rails paperclip ruby-on-rails-3

希望有人可以帮忙!

利用Paperclip将文件上传到应用程序,这是我需要涵盖的两件事.

1)链接到File for Download的最佳方式是什么,我目前正在链接到公共文件夹,但它不是最好的选项,因为它显示了我不想要的URL.想想也许button_to调用不确定它是否已经构建到Paperclip中.选项.

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

请帮助米勒!

Ric*_*eda 12

我会回答Nº2的问题.
假设你的模型叫做gallery.
在您的图库控制器中,您将添加下载方法:

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
Run Code Online (Sandbox Code Playgroud)

现在,从您的路由中,您将调用root到此方法:

match 'gallery/:id' => 'gallery#download', :as => :download
Run Code Online (Sandbox Code Playgroud)

在您的观点中:

- @galleries.each do |gallery|     
  = link_to 'Download Picture', download_path(gallery.id)
Run Code Online (Sandbox Code Playgroud)

我不在家,我无法测试此代码,但您可以访问我用与您相同的问题做的那些问题.如果它能解决您的问题,请告诉我.

Rails send_file来自Paperclip的多个样式,如何避免代码重复?
Rails 3.0关联模型,下载'保存到'图像