Rails CSV附件在浏览器中打开,而不是下载 [英] Rails CSV attachment opens in browser instead of downloading
问题描述
在我的Rails 3应用程序中,我提供了一个CSV模板供用户下载.无需下载它,只需在新选项卡中打开文本即可.这是代码:
In my rails 3 application I provide a CSV template for users to download. Instead of downloading it simply opens the text in a new tab. Here is the code:
<a href="/templates/myFile.csv" target="_blank">click here</a>
这以前对我有用,我不确定有什么变化可以打破它.与此类似的其他问题则建议添加target='_blank'
或更改标题,但我这样做并没有太大的运气.
This was working for me previously and I'm not sure what could have changed to break it. Other questions similar to this suggest adding target='_blank'
or changing the headers but I haven't had much luck doing so.
我不认为这会有所作为,但是此链接位于人体的模态内部,
I don't think it should make a difference, but this link is inside of a modal who's body comes from here:
<div class="modal hide" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<%= modal_header "Help" %>
<div class="modal-body">
<%= render 'help' %>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
推荐答案
您可以使用send_file方法:
You can use send_file method:
def download_file(file_path)
send_file(file_path, :type => 'text/csv', :disposition => "attachment")
end
您可以将此方法添加到控制器并进行路由,然后在您的视图中使用它:
You can add this method to a controller and routes, then use it in your views:
link_to "Download", download_file_path('some_file.csv')
这篇关于Rails CSV附件在浏览器中打开,而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!