简体   繁体   English

Rails / Haml打开链接为“另存为”

[英]Rails/Haml open link as 'save as'

I must serve some files for direct download by the user. 我必须提供一些文件供用户直接下载。 But if they are some common types(like .mp3, .pdf) the client browser usually tries to show them directly. 但如果它们是一些常见类型(如.mp3,.pdf),则客户端浏览器通常会尝试直接显示它们。 Which is not the implied behaviour. 这不是隐含的行为。 I need some helper method which will not show the files, but will open 'save as' dialogue window. 我需要一些不显示文件的辅助方法,但会打开“另存为”对话窗口。

Something like 就像是

= link_to_save_as "Download file", @resource.url

Is there some ready helper method in Rails? 在Rails中有一些现成的辅助方法吗?

If there is not, is there some gem providing that functionality? 如果没有,是否有一些宝石提供该功能?

If there is not, is it possible to implement it by myself (if the browsers does not forbid this, somehow)? 如果没有,是否可以自己实现它(如果浏览器不以某种方式禁止这个)?

You might want to look at adding send_file to the a controller action. 您可能希望将send_file添加到控制器操作中。 See docs here. 请参阅此处的文档

Probably something like: 可能是这样的:

def download_file(filename)
  # You obviously need some restrictions on what can be requested 

  send_file '/path/to.mp3', :disposition => 'attachment'
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM