0
votes

I am using angular with laravel to generate some pdf files. I post some values to server and server responds with a file. now how to download the file from response.

i tried this method its not working

controller

Ecs.genSingleEcs(data).success(function(response){

    var file = new Blob([response], {type: 'application/pdf'});
    var fileURL = URL.createObjectURL(file);
    $scope.ecsurl = fileURL;
});

view

<button class="btn btn-primary" ng-href="{{ ecsurl }}">Download</button>

response object

%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�3R��2�35W(�r Q�w3T04�30PISp �Z*�[����(hx����+���(j*�d���7W endstream endobj 1 0 obj <

> endobj 5 0 obj < endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 5 0 R > /XObject << > > endobj 6 0 obj << /Producer (FPDF 1.7) /CreationDate (D:20141126070253) > endobj 7 0 obj << /Type /Catalog /Pages 1 0 R > endobj xref 0 8 0000000000 65535 f 0000000228 00000 n 0000000416 00000 n 0000000009 00000 n 0000000087 00000 n 0000000315 00000 n 0000000520 00000 n 0000000595 00000 n trailer << /Size 8 /Root 7 0 R /Info 6 0 R > startxref 644 %%EOF

1

1 Answers

0
votes

I suggest you to use: https://github.com/eligrey/FileSaver.js/

And add this line of code to yours:

var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
saveAs( blob, "myFileName.pdf");

I am already using this in my AngularJS app