I trying to download SSRS report in excel format from my MVC application.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + reportName + "." + extension);
Response.BinaryWrite(result);
Response.Flush();
Response.End();
But i am getting this error as -
System.Web.HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.
I tried changing Response.Buffer to BufferOutput but still getting the same error. What am I missing here?
In the code below, i tried to download an excel file as -
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader($"content-disposition", "attachment;filename=" + "{reportFileName}" + ".xlsx");
using (System.IO.MemoryStream MyMemoryStream = new MemoryStream())
{
MyMemoryStream.Write(result, 0 , result.Length);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
return File(MyMemoryStream.ToArray(), "application/octet-stream");
}
User contributions licensed under CC BY-SA 3.0