Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Created June 5, 2015 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MarkLavrynenko/5b763e36b128170cdb77 to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/5b763e36b128170cdb77 to your computer and use it in GitHub Desktop.
Download Pdf file using AJAX (angular)
angular.module("test-app", [])
.controller("PDFDownloadController", ["$scope", "$http", function($scope, $http) {
function handleResponse(response){
var pdfFile = new Blob([response.data], { type : 'application/pdf' })
var downloadURL = URL.createObjectURL(pdfFile);
var link = document.createElement('a');
link.href = downloadURL;
link.download = "preview.pdf"
document.body.appendChild(link);
link.click();
}
$scope.loadPdf = function () {
$http.get("file.pdf", { responseType : 'arraybuffer' }).then(handleResponse);
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment