0

I want to download a PDF from server depending on the input request sent from UI . I am getting a pop-up with [object objcet] written on it . Can somebody help me out in this . P.S. : I have to use $resource only , not the $http one , thanks :)

This is my Service call to WEB API , Service name is "GetInvoice"-->

function ($resource) {
    return $resource('app/rest/invoice/:id', null, {
             'update': { method:'PUT'}
        },{ headers: {'Content-Type': 'application/pdf'}, 
    transformRequest: []});
                     }

This is my service call from controller -->

GetInvoice.update({id:'1'},angular.toJson(invoiceJSON)).$promise.then(function (data) { 

          var file = new Blob([data]);
          var fileURL = ($window.URL || $window.webkitURL).createObjectURL(file);
          $window.open(fileURL, '_blank', 'download');});
Anshu_06
  • 3
  • 1

1 Answers1

0

Change the line

var file = new Blob([data]);

to

var file = new Blob([data.data]);

and check out.

If this doesn't work out,

dynamically create a anchor tag and invoke the click event which will open a popup / new tab with the content. Refer here

Naveen Kumar G C
  • 1,332
  • 1
  • 10
  • 12
  • Hi Naveen, thanks for your help . In the URL you shared, 'responseType' is mentioned at 2nd place after API URL as its a 'get' service . How to do this in 'put' request ? – Anshu_06 Mar 19 '18 at 10:48
  • actually my error seems to be in 'data' in controller class . I am not getting appropriate value in this . It could be because I am not handling response properly in service class . – Anshu_06 Mar 19 '18 at 13:28
  • Yes. put a break point in the debugger and then watch it to see the data. [Object object] actually seems that you are working with the object and not with the data inside it. mark the answer are correct if this works out for you. – Naveen Kumar G C Mar 20 '18 at 11:04