How To Download Excel | File From Byte Array In C# |top|
[HttpGet("direct-download")] public async Task DownloadDirectly() { byte[] excelBytes = GetExcelData(); HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=Report.xlsx"); await HttpContext.Response.Body.WriteAsync(excelBytes, 0, excelBytes.Length); } Use code with caution. Important: Handling Large Excel Files (OOM Exceptions)
By passing a filename into the File() constructor, ASP.NET sets the content-disposition header to attachment , which forces the browser to prompt the user to save the file rather than trying to open it in a browser plugin. Method 2: Manually Writing to Response Stream how to download excel file from byte array in c#
Always use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for .xlsx files. how to download excel file from byte array in c#


