本文介绍了点击下载文件 - Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用Rails 2后端,Heroku进行托管,用于文件上传的Paperclip和用于文件存储的Amazon S3。

My application is using Rails 2 backend, Heroku for hosting, Paperclip for file uploads, and Amazon S3 for file storage.

现在用户可以使用纸夹上传文件+ s3 - 这完美无瑕。上传后,其信息板上会显示一个图标,链接到文件位置(以s3为单位)。当点击图标时,浏览器会在新窗口中打开文件(对于大多数文件类型 - PDF,MP3,img等)。当用户点击文件的图标(如Gmail附件)时,我想要自动下载该文件。解决方案应该可以适用于任何文件类型和跨浏览器。

Right now users can upload files with paperclip + s3 - this works flawlessly. After upload, an icon appears on their dashboard, linked to the file location (in s3 bucket). When the icon is clicked, the browser opens the file in a new window (for most file types - PDF, MP3, img, etc). Instead of opening, I want the file to be automatically downloaded when the user clicks the file's icon (like Gmail attachments). The solution should be able to work for any file type and cross-browser.

有没有帮助在rails中执行此操作,还是需要javascript?我真的坚持在这一个,所以任何东西,指出我在正确的方向将不胜感激。谢谢!

Is there a helper to do this in rails, or is javascript needed? I'm really stuck on this one so anything to point me in the right direction would be greatly appreciated. Thanks!

推荐答案

请尝试以下操作:

class Test < ActiveRecord::Base

  has_attached_file :testfile,
    :storage => :s3,
    # All your S3 config
    :s3_headers => {"Content-Disposition" => "attachment"}

end

这应该告诉Paperclip Gem设置Content-Disposition标题为新上传的文件的值附件。

This should tell the Paperclip Gem to set the "Content-Disposition" header to the value "attachment" for newly uploaded files.

请注意,您必须手动编辑已上传的文件,例如与Cyber​​duck或另一个FTP客户端。

Note that you have to manually edit the already uploaded file, e.g. with Cyberduck or another FTP Client.

这篇关于点击下载文件 - Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:28