2017-01-23 251 views
0

如何觸發一個csv文件從一個按鈕點擊Angular內下載?Angular請求csv文件下載url,並強制下載

後端是php路由。

我嘗試了一種解決方案,它動態地添加一個錨點標記,並附加到它的href屬性,從http請求獲取csv文件內容。但它不適用於我...還因爲我們將csc內容附加到href屬性,我擔心它允許多少內容的href長度。

回答

0

在擺弄現有的答案後,我嘗試了下面的工作。

採用了棱角分明1.5.9

我做了它的工作就這樣被window.location的設置CSV文件的下載網址。經過測試,並與最新版本的Chrome和IE11一起工作。

$scope.downloadStats = function downloadStats{ 
     var csvFileRoute = '/stats/download'; 
     $window.location = url; 
    } 

HTML

<a target="_self" ng-click="downloadStats()"><i class="fa fa-download"></i> CSV</a> 

在PHP設置了響應下面標頭:

$headers = [ 
    'content-type'    => 'text/cvs', 
    'Content-Disposition'  => 'attachment; filename="export.csv"', 
    'Cache-control'    => 'private, must-revalidate, post-check=0, pre-check=0', 
    'Content-transfer-encoding' => 'binary', 
    'Expires' => '0', 
    'Pragma' => 'public', 
]; 
+0

'文本/ cvs'或'文本/ csv'? –