2015-09-07 23 views
2

我想下載按鈕點擊一個文件中angularjs如何下載一個文件angularjs控制器

<button ng-click=downloadfile()>download</button> 

在控制器

$scope.downloadfile=function() 
     { 
      // 
     } 

我在downloadfile函數寫哪個功能請你解釋

+0

我希望這Usesfull http://stackoverflow.com/questions/14215049/how-to-download-file - 使用 - angularjs和呼-MVC-API –

回答

5

在您的downloadFile()函數中寫出。

//Initialize file format you want csv or xls 
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);   

    //this trick will generate a temp <a /> tag 
    var link = document.createElement("a"); 
    link.href = uri; 

    //set the visibility hidden so it will not effect on your web-layout 
    link.style = "visibility:hidden"; 
    link.download = fileName + ".csv"; //this is an example file to download, use yours 

    //this part will append the anchor tag and remove it after automatic click 
    document.body.appendChild(link); 
    link.click(); 
    document.body.removeChild(link); 
0

$ scope.downloadFile =功能(DOC){

 var token = $localStorage.authenticationToken || $sessionStorage.authenticationToken; 
     if (token) { 
      token = 'Bearer ' + token; 
     } 

     var url = new $window.URL($location.absUrl()).origin + "/api/voyages/" + 
      vm.voyage.id + '/documents/' + doc.id + '?auth=' + token; 
     $window.open(url); 
    }; 
相關問題