I am trying to download an Excel file through the browser but when I open this file, I get the error in Excel that the file is corrupt.
If I save this file through the backend locally to a directory on my computer, I can open the file and see data in it with no issues. The corruption issue appears only when I download the file through the browser.
Below is what I have tried so far but still didn't manage to get it fixed.
Can someone help regarding this issue?
Thank you.
EDIT: This is a
screenshot of the result of the "
console.log(data)
", maybe it will provide an extra clue about what is going wrong.
What I have tried:
For testing purposes and trying to fix the issue, I am just generating an empty Excel file based on a template in the backend, then passing it to the frontend.
File1.cs
using var workBook = XLWorkbook.OpenFromTemplate(templatePath);
`
`
workBook.SaveAs(memoryStream);
return memoryStream;
}
Controller.cs
[HttpGet]
public async Task<IActionResult> SaveAction([FromQuery] int? propertyId)
{
`
`
stream.Position = 0;
stream.Flush();
return File(stream.ToArray(), "text/xls");
}
frontendFile.js
This is the js file. I also tried both
contentTypes
here, but neither changed the end result and file corruption issue.
$.get(_options.saveCoversUrl, function (data) {
console.log("file to frontend sent2");
console.log(data);
var a = document.createElement('a');
data = new Blob([data], { type: "text/xls" });
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'sample.xlsx';
a.click();
window.URL.revokeObjectURL(url);