In my AngularJS code, I need to download an Excel file by making an HTTP POST request to a Java Rest API. The API returns the file with the header "Content-Disposition" : "attachment; filename=\"new_excel_file.xlsx\""
Java Code
@Post
@Path("/excel/trekResult")
@Produces("application/vnd.ms-excel")
public Response getResultsReport(@HeaderParam(Constants.ID) Long userId, @QueryParam(Constants.COMPANY_TREK_ID) Integer companyTrekId) {
String CONTENT_DESPOSITION = "Content-Disposition";
String CONTENT_ATTACHEMENT = "attachment; filename=\"new_excel_file.xlsx\"";
//Generates an Excel file in the local file system
File excelFile = misHelper.writeToFile(workBook, mis, userId, "trek-results");
return Response.ok().entity((Object)excelFile).
header(CONTENT_DESPOSITION, CONTENT_ATTACHEMENT).build();
}
On the JavaScript Side
myService.exportResult($scope.companyTrek.id).then(function(result) {
if(result !== undefined || result !== '') {
var blob = new Blob([result], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
var objectUrl = URL.createObjectURL(blob);
saveAs(blob, 'Trek-Results-'+fetchCurrentDate()+ '.xlsx');
}
}
Used FileSaver.js to save the file.
The resulting file is [Object, Object]
Tested the locally generated file.
For more information on a similar question that didn't solve my issue, please refer to this link:
Receive an Excel file as response in JavaScript from a REST service