Angular4: How to retrieve values in Response Headers

0

I have a WebService that returns a PDF file (byte []). I am returning the file name in the header of the response.

In Chrome I can see the 'Response Headers' values and see that all headers are there, including the file name. However, when I try to retrieve these values in Angular, I can not, because in headers comes only the "Content-Type: application / pdf".

getPdf(url: string){

    this.http.get(url, {responseType: 'arraybuffer', observe:'response'})
        .subscribe(data => {
            console.log(data.headers.get('filename')); // Imprime: null
            console.log(data.headers.keys()); // Imprime: ["Content-Type"]
            this.downloadFile(data.body);
    });
  }

Response Headers displayed in browser:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Request-Width, Content-Type, Accept, authCode
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Disposition:attachment; filename=arquivo.pdf
Content-Type:application/pdf
Date:Mon, 26 Nov 2018 14:23:37 GMT
filename:arquivo.pdf
Server:JBoss-EAP/7
Transfer-Encoding:chunked
X-Powered-By:Undertow/1

Does anyone know what's wrong? How do I retrieve this value?

    
asked by anonymous 26.11.2018 / 19:33

1 answer

0

I discovered the problem. In the return of my service, I need to define the Headers that I want exposed with the value 'Access-Control-Expose-Headers'. If you do not set this, the Angle ignores past values.

For example, my webservice in java was:

protected Response buildFile(Status status, byte[] file, String nomeArquivo) {
        return Response.status(status)
                .entity(file)
                .header("Access-Control-Allow-Credentials", "true")
                .header("Access-Control-Expose-Headers", "Content-Disposition, filename")
                .header("Content-Type", "application/pdf")
                .header("Content-Disposition", "attachment; filename="+ nomeArquivo)
                .header("filename", nomeArquivo)
                .build();
    }

More information about: link

    
26.11.2018 / 21:02