2012-10-08 133 views
1

在我的rails 3应用程序中,我提供了一个供用户下载的CSV模板。而不是下载它只是在新标签中打开文本。这里是代码:Rails CSV附件在浏览器中打开而不是下载

<a href="/templates/myFile.csv" target="_blank">click here</a> 

这是我以前的工作,我不知道什么可以改变打破它。其他与此类似的问题表明添加target='_blank'或更改标题,但我没有太多运气。

我不认为它应该有所作为,但这个链接是一个模式谁是身体内部的来自这里:

<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> 

回答

5

可以使用由send_file方法:

def download_file(file_path) 
    send_file(file_path, :type => 'text/csv', :disposition => "attachment") 
end 

您可以将这个方法添加到控制器和路线,然后在你的看法使用它:

link_to "Download", download_file_path('some_file.csv') 
0

目标=“_空白”将在新标签中打开该文件。但它取决于浏览器设置是打开到选项卡中的文件还是询问文件是否被下载。

相关问题