I am using the EPPlus on my webapi core to return a FileContentResult. When the object returns to my Angular v12 app, I get the following error when I try to open my excel file
Excel cannot open the file because the file XXX-8_3_2021.xlsx format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
I following a few old post on how to implement export to Excel and save the downloaded Excel.
Any help is appreciated.
[HttpPost("Export")]
public async Task<ActionResult<ActionResult<FileContentResult>>> Export([FromBody] ExportDto request)
{
if (request != null)
{
byte[] fileBytes = await GetExcelFile(request);
return Ok(
File(fileBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"MyExcel-" + DateTime.Now.ToShortDateString() + ".xlsx"));
}
else
{
return BadRequest(responseMessage);
}
}
My Angular Service is
export(dto: ExportDto): Observable<ExportDto> {
return this.http.post<any>(this.exportUrl, dto)
.pipe(map(
response => {
return this.handleFileDownload(response, dto)
}
));
}
private handleFileDownload(response: any, dto: ExportDto) {
let file = new Blob([response.fileContents],
{type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
let fileName = response.fileDownloadName;
saveAs(file, fileName);
return dto;
}
I tried but with http.post, the responseType is throwing an error
this.http.post<any>(this.exportUrl, dto, { responseType: "arrayBuffer"})