I have a html Button and hitting a WebMethod via JSON. I have used ClosedXML plugin to create excel Workbook on the run and saved it to MemoryStream. Then converted it to bytes. I am getting bytes also.
But HttpContext.Current.Response.BinaryWrite(excelBytes) is not writing bytes to browser. The file is not getting downloaded. Not showing any error. Code is executing without any error. But file is not downloading.
The web method is too long therefore I am providing the only that part which is creating issue:
byte[] excelBytes;
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
excelBytes = MyMemoryStream.ToArray();
//MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
}
//HttpContext.Current.Response.OutputStream.Write(excelBytes, 0, excelBytes.Length);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
//HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=SummaryReportExport_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".xlsx");
HttpContext.Current.Response.AddHeader("Content-Length", excelBytes.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(excelBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
wb in above code is the object of WorkBook class.