Reputation: 407
I'm working on an ASP.NET Core project targeting .NET 5.
I have this method returning an ActionResult
:
public async Task<ActionResult> ExportMarksForm(int testId)
{
var stream = new MemoryStream();
// Some logic to Save the workbook as stream
return File(stream, "application/ms-excel", "xxx.xlsx");
}
The problem
Everything works fine, but when I downloaded the file, I got an Excel message that told me that the file is damaged, that is means an error happened or the file writing operation not complete.
I tried breakpoints and I saw that the stream
variable has bytes
.
So, please how can I fix this issue? Or if there is any other solution to return a stream
as an Excel file please share it with me.
Upvotes: 5
Views: 7025
Reputation: 407
I fixed the Issue by using FileStreamResult
:
return new FileStreamResult( stream , "application/ms-excel" )
{
FileDownloadName = "xxx.xlsx"
};
Upvotes: 12