回形针力下载 [英] Paperclip Force Download

查看:49
本文介绍了回形针力下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望任何人都可以帮助解决这个问题!

Hope anyone can help on this!

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

Making use of Paperclip to upload Files to an Application, so two things that i need to cover.

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

1) What is the best way to link to the File for Download, I am currently linking to the public folder but it is not the best option as it shows the URL which i dont want. Thinking maybe a button_to call be not sure if this is build into Paperclip already. Options.

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

2) When above is done then the Browser needs to force to download not just open the file, or at least give the user the standard firefox option open or save.

请协助谢谢米尔!!!

推荐答案

我来回答问题 2.
假设您的模型名为画廊.
在您的画廊控制器中,您将添加一个下载方法:

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

在您看来:

- @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来自 Paperclip 的样式,如何避免代码重复?
Rails 3.0 关联模型与下载保存到"图像

这篇关于回形针力下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆