[英]How to give the xls file link in Ruby on Rails?
How to give the xls file link in ruby. This is my file path
link_to "Excel", "/#{RAILS_ROOT}/public/reports/10014_ByNetwork.xls", :target=>"_blank"
當我給出上面這樣轉換的鏈接時
<a href="//home/kiran/shekar/wavespot/public/reports/10014_ByNetwork.xls" target="_blank">Excel</a>
所以它不起作用。 其實我需要這樣
<a href="file:///home/kiran/shekar/wavespot/public/reports/10014_ByNetwork.xls" target="_blank">Excel</a>
請給我確切的路徑...
@Gagan您的語法不正確。 在發布答案之前,您應該先對其進行測試。 這是正確的方法:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", target: "_blank" %>
要么
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", :target=>"_blank" %>
在第二個雙引號引起來之后,您錯過了逗號。
您確定需要file://鏈接嗎? 它不是由Rails服務器操作的,只能在您的機器上使用。 生成這樣的鏈接可能會更好:“ http://you_server_url.org/public/file.xml ”。 它在本地和遠程應用程序上都可以正常工作,並且您可以通過控制器操作來管理文件發送。
在您的視圖文件中嘗試以下操作:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls" :target=>"_blank" %>
如果您要下載多個文件,並且必須在視圖中使用不同的鏈接,我也可以建議您使用下一種方法:
添加下載xls 獲取資源並將路由助手添加到您的routes.rb
文件,例如:
get "downloads/xls/:id" => "downloads#xls", :as => :download_xls
在您的控制器中,對於我的示例,我將使用app/controllers/downloads_controller.rb
我們將需要添加xls操作以使用send_file
流數據:
def xls
if params[:id]
send_file("#{Rails.root}/public/reports/#{params[:id]}.xls",
filename: "#{params[:id]}.xls",
type: 'application/excel',
disposition: 'attachment')
end
end
您可以在此處了解更多信息: http : //apidock.com/rails/ActionController/DataStreaming/send_file
最后,在您看來,您將使用link_to
幫助器以及上面聲明的download_xls_path
路由,並將文件名用作參數:
<p>
Click to download: </br>
<%= link_to "NameOfYourXlsFile.xls", download_xls_path(NameOfYourXlsFile) %>
</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.