2017-09-19 100 views
0

我想下載我的本地機器中的文件。每當我點擊下載鏈接,它給的網址在瀏覽器不安全:http://localhost:8080/MYOB_Fabric/%7B%7B無法在angularjs中下載文件

HTML代碼:

<div ng-controller="MainCtrl"> 
    <table class="table2" border=1 frame=void rules=rows> 
     <tr> 
      <th colspan=3>Transaction Details</th> 
     </tr> 
     <tr> 
       <th>Hash</th> 
      </tr> 
      <tr ng-repeat="transaction in transactionDetails "> 
       <td><a ng-href={{ fileUrl }} download="logFile.txt">{{ transaction.Hash }}</a></td> 
      </tr> 
    </table> 
    </div> 

JS代碼:

var appHlVl = angular.module('hlfabric', [ 'ngRoute' ]); 
appHlVl.config(['$compileProvider', function($compileProvider) { 
     $compileProvider.aHrefSanitizationWhitelist(/^\s*(|blob|):/); 
    }]); 

appHlVl.controller('MainCtrl', function($scope, $rootScope,$http,$window,$sce) { 
    var txtfileURL = '/home/rajasekhar/peer-log.txt'; 
    $http.get(txtfileURL, { responseType: 'arraybuffer' }) 
    .then(function success(response) { 
     var file = new Blob([response.data], { 
      type: 'text/plain' 
     }), 
     url = $window.URL || $window.webkitURL; 
     $scope.fileUrl = $sce.trustAsResourceUrl(url.createObjectURL(file)); 
    }); 
}); 

回答

0
<a ng-href="{{fileUrl}}" target="_blank">{{ transaction.Hash }}</a> 

或app.config文件放此行

$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension):/); 
+0

目標=「_空白」它會打開一個選項卡,你把目標=「_自我」它會打開同一頁 –

+0

我沒有面對問題與「目標」屬性。我無法下載本地文件。 –

+0

fileurl包含localfile的路徑並不意味着fileurl =「https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS7VE_YDzLkWX504oXOjLdZwqeatfr7654-kRfG_zyh2_n6GPZvtG9fC82z」 –

0

試試這個

app.config(['$compileProvider', function($compileProvider) { 
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|tel):/); 
}]);