Download a file with AngularJS -


i need provide link download file, link must hidden , accessible users, here code , there no errors whatsoever, can't download dialog box open:

template

 <a ng-href="#" target="page" type="button" class="btn"  ng-click="download()">download</a> 

script file

$scope.download = function(){  //here need know code,can explain me } 

first of all, can't "hide/not public" link in web based technology (html/css/javascript) application. downloads handled client, download/link-url must public. can try "hide" protective params e.g. ids in download url using backend executed programming language "php or node.js, etc.". in way can create hash urls http://www.myside.com/download/359ftbw!s3t387ihs hide parameters recordid in url.

by knowing this, solution pretty easy. use html attribute download <a href="http://mydownloadurl" download>link text</a> force browser download href source. no ng-click needed here. unfortunately download attribute not supported safari browser. doesn't realy matter while browser handling download itself. depending on users system os configuration file downloaded or directly opened in programm installed on system. example, pdf file opened in pdf viewer if pdf viewer application available.

i wrote plunker handles ng-href in angularjs controller $scope. hope need.

your controller:

var app = angular.module('plunker', []);  app.controller('mainctrl', function($scope) {   $scope.filehref = 'http://www.analysis.im/uploads/seminar/pdf-sample.pdf'; }); 

your view:

<head>   <meta charset="utf-8" />   <title>angularjs plunker</title>   <script>     document.write('<base href="' + document.location + '" />');   </script>   <link rel="stylesheet" href="style.css" />   <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>   <script src="app.js"></script> </head>  <body ng-controller="mainctrl">   <a ng-href="filehref" download="yourfilename">download</a> </body> </html> 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -