2012-10-08 131 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

目標=「_空白」將在新標籤中打開該文件。但它取決於瀏覽器設置是打開到選項卡中的文件還是詢問文件是否被下載。

相關問題