How can I initiate a download in Angular 2 when the backend is already providing the download in the response?
The backend has an API endpoint set up like this:
Workbook userExcelReport = excelReportGenerator.createUserExcelReport(userSurveys);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"" + userFileName + ".xlsx\"");
try {
userExcelReport.write(response.getOutputStream());
userExcelReport.close();
response.flushBuffer();
return new ResponseEntity(HttpStatus.OK);
} catch (IOException e) {
logger.error("The excel workbook could not write to the outputStream ", e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
This method works well when accessed directly without any frontend, but now I want to build an Angular 2 page that triggers the download.
I have made some progress on the page and this is the service code snippet I currently have:
return this._http.get(this.url, {
search: params
});
However, I am unsure of how to properly trigger the download using the response from the backend. I need help figuring out how to map it or what steps should be taken.