Skip to content

Instantly share code, notes, and snippets.

@Kamilnaja
Last active January 29, 2018 08:14
Show Gist options
  • Save Kamilnaja/472fd6f48aed32e2e7780af0e395a29a to your computer and use it in GitHub Desktop.
Save Kamilnaja/472fd6f48aed32e2e7780af0e395a29a to your computer and use it in GitHub Desktop.
angular.module("my-app", ['dx', 'ngFileSaver'])
.controller("app-ctrl", ['$scope', '$http', function ($scope, $http) {
$scope.downloadPDF = function downloadPDF() {
$http({
url: 'http://localhost:3000/data',
method: 'GET',
})
.then((res) => {
console.log(res.data.lorem);
$scope.pdf = 'data:application/octet-stream;base64,' + res.data.lorem;
}).then(() => {
var dlnk = document.getElementById('downloadLink');
dlnk.href = $scope.pdf;
dlnk.click();
})
};
);
<!DOCTYPE html>
<html lang="en" ng-app="my-app">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<script type="text/javascript" src="node_modules/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="node_modules/devextreme/dist/css/dx.common.css" />
<link rel="stylesheet" type="text/css" href="node_modules/devextreme/dist/css/dx.light.css" />
<script src="node_modules/angular/angular.min.js"></script>
<script type="text/javascript" src="node_modules/devextreme/dist/js/dx.all.js"></script>
<script src="node_modules/angular-file-saver/dist/angular-file-saver.bundle.js"></script>
<script src="app.js"></script>
<title>Document</title>
</head>
<body>
<div ng-controller="app-ctrl">
<!-- test -->
<button ng-click="downloadPDF()">Pobierz</button>
<a id="downloadLink" download='myDoc.pdf' style="display: none" />
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment