Hello All
Here I need to download multiple Excel sheets i use for loop. after one iterate loop I got am Exception like below
Server cannot append header after HTTP headers have been sent.
try
{
for(some condition)
{
//some code grid view//
Response.ClearContent();
Response.Buffer = true;
long lnUserID = clsLists.getCurrentUserId();
Response.AddHeader("content-disposition", "attachment; filename=Release-" + ReleaseEMCTSDB.users.SingleOrDefault(u => u.UserId ==
l nUserID).UserName + "-" + DateTime.Now.ToString() + ".xls");
Response.AppendHeader("Name", "Value");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string readergridview = sw.ToString();
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Resp onse.Flush();
Response.End();
}
}
try
{
Exception
}
Please help me it is possible to iterate loop
Hari BPosted Sep 3, 2015, 6:25 AM
Blackbarbie BbPosted Sep 3, 2015, 5:57 AM
checkout this link:
http://www.4guysfromrolla.com/articles/092910-1.aspx[^]
The problem here is client can make only 1 request at a time and logically for 1 request there should be 1 response. The header of the request can be sent only once and before every other body data of the response and once it is sent the client browser starts taking action acording to the header received for that request.
when you Flush() the response it sends your header and the data added to the output stream.
Plus i dont understand how will a client can request multiple files in a single click? there must be a link on his screen reads like "Download All" or something similar. And if anyhow you are able to allow multiple file download how will a client manage when he will continously get 20-25 files to download/save/open on after one?
so the best answer is comppress all files in a zip and send the compressed zip to client.