Home > Software engineering >  downloading XML in angular
downloading XML in angular

Time:12-27

I'm trying to make a method which download the return the API to XML file.

I did this method to make the request in the API:

public getXML(ano:any,mes:any,glosadas:any):Observable<Blob>{
const token = this.token.retornaToken();
const headers = new HttpHeaders({Authorization:`Bearer ${token}`});

return this.http.get(`API/xml?TpoRelatorio=1&NroGspContratado=&NroProtocolo=&NmeCliente=&SomenteGlosadas=${glosadas}&Ano=${ano}&Mes=${mes}&Page=1&Count=999999999`,{ headers, responseType:'blob' })}

I thought that was just needed to call getXML() in component.ts, but it didn't worked...

What should I do in component.ts?

CodePudding user response:

The solution for me was using the Blob object.

The method:

this.homeService
  .getXML(this.ano,this.mes,this.glosadas)
  .subscribe(data =>{
  var element = document.createElement('a');
  var blob = new Blob([data],{
    type:'text/xml'
  })
  var url = URL.createObjectURL(blob);
  element.href = url;
  element.setAttribute('download','file.xml');
  document.body.appendChild(element);
  element.click();
})
  • Related