Download a PDF received from an API with Angularjs

Asked

Viewed 920 times

2

I have an API that returns me a PDF file.

I want to make the angle hit this route and force the download of the PDF file.

Today I do so:

var blob = new Blob([arquivo], {type: "application/pdf"});
saveAs(blob, 'arquivo.pdf');

The download works, but the pdf file comes blank. If I hit the route manually, the file comes complete.

1 answer

3


The responseType in the request.

The problem was not at this point, but at the request call, where the GET was made, it was missing to specify the responseType.

That’s how the code works:

$http.get('/api/download/' + uid, {responseType:'arraybuffer'});
  • With this method you can download in IE?

  • @Guilhermepatriarch from IE 10 only up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.