Download a file generated in the backend by Angular2 +

0

Hello, I'm having problems downloading a backend-generated file in my angled project, I have the following method

generateXlsx(obj) {
let headers = new Headers();
let token = localStorage.getItem('token');
let client = localStorage.getItem('client');
let uid = localStorage.getItem('uid');
headers.append('access-token', token);
headers.append('client', client);
headers.append('uid', uid);
return this.http.get(this.xlsxUrl, { params: obj, headers: headers });
}

When it is called it goes to Url to download my file, however when I call this route / Url via angular, it does not download, however in the Network tab the file is saved in response, follow print of the result

InmyFrontEnd,whenIhitabuttonandcalledthefollowingmethod

generatePlanilha(obj){this.api.generateXlsx(obj).subscribe(response=>{if(response){console.log("Planilha Gerada");
    }
  },
    (error => { console.log("NAO GEROU PLANILJA") }));
  }

I wanted to know how do I download the answer that is in the Network

    
asked by anonymous 03.12.2018 / 17:07

1 answer

0

You can create a blob from the response and download it by opening a popup with the data:

 import 'rxjs/add/operator/map';

 .map((res:Response) => res.text())
 .subscribe(response => {
    if (response) {
      const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const url= window.URL.createObjectURL(blob);
      window.open(url);
    }
  },
    (error => { console.log("NAO GEROU PLANILJA") }));
  }
  

link

     

link

     

link

    
03.12.2018 / 17:14