Server cannot append header after HTTP headers have been sent c# excel inline -


i have problem trying return excel inline response.

i searched through coding pages answers , can not find works yet.

i made simplified example of doing below. issue happen.

error in line:

line 56:         response.addheader("content-disposition", 

code behind:

public partial class _default : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {         pleasewait();         call();     }      protected void call()     {         strsql = "select * ....... ; ";          sqlcommand cmd = new sqlcommand(strsql);         datatable dt = getdata(cmd);          gridview gridview1 = new gridview();         gridview1.allowpaging = false;         gridview1.datasource = dt;         gridview1.databind();          response.clear();         response.buffer = true;         response.bufferoutput = true;         response.clearheaders();          response.addheader("content-disposition",          "attachment;filename= "output.xls");         response.charset = "";         response.contenttype = "application/vnd.ms-excel";         stringwriter sw = new stringwriter();         htmltextwriter hw = new htmltextwriter(sw);          (int = 0; < gridview1.rows.count; i++)         {             gridview1.rows[i].attributes.add("class", "textmode");         }         gridview1.rendercontrol(hw);          string style = @"<style> .textmode { mso-number-format:\@; } </style>";         response.write(style);         response.output.write(sw.tostring());         response.flush();         response.end();     }      protected void pleasewait()     {         this.response.write("<meta http-equiv=x-ua-compatible content=ie=8>");         this.response.write(@"<style type=text/css media=all>");         this.response.write(@".loading");         this.response.write(@"{");         this.response.write(@"text-align: center;");         this.response.write(@"padding-top: 30px;");         this.response.write(@"border-width: 1px solid #000;");         this.response.write(@"width: 300px;");         this.response.write(@"height: 100px;");         this.response.write(@"-ms-filter: alpha(opacity=90);");         this.response.write(@"-ms-opacity: 0.90;");         this.response.write(@"border-style: solid;");         this.response.write(@"background-color: #ffffff;");         this.response.write(@"position: absolute;");         this.response.write(@"font-family: trebuchet ms;");         this.response.write(@"font-size: small;");         this.response.write(@"position: absolute;");         this.response.write(@"top: 0;");         this.response.write(@"bottom: 0;");         this.response.write(@"left: 0;");         this.response.write(@"right: 0;");         this.response.write(@"margin: auto;");         this.response.write(@"display: block;");         this.response.write(@"background: url('images/wait01.gif') no-repeat center;");         this.response.write(@"}");         this.response.write(@"</style>");          this.response.write(@"<div id=mydiv class=loading>&nbsp;</div>");         this.response.write(@"<script>mydiv.innertext = '';");         this.response.write(@"</script>");         this.response.write(@"<script language=javascript>;");         this.response.write(@"var dots = 0;");         this.response.write(@"var dotmax = 10;");         this.response.write(@"function showwait()");         this.response.write(@"{");         this.response.write(@"var output;");         this.response.write(@"output = 'wait...';");         this.response.write(@"dots++;");         this.response.write(@"if(dots>=dotmax)dots=1;");         this.response.write(@"for(var x = 0;");         this.response.write(@"x < dots;x++)");         this.response.write(@"{");         this.response.write(@"output += '.';");         this.response.write(@"}");         this.response.write(@"mydiv.innertext =  output;");         this.response.write(@"}");         this.response.write(@"function startshowwait()");         this.response.write(@"{");         this.response.write(@"mydiv.style.visibility = 'visible'; ");         this.response.write(@"window.setinterval('showwait()',1500);");         this.response.write(@"}");         this.response.write(@"function hidewait()");         this.response.write(@"{");         this.response.write(@"mydiv.style.visibility = 'hidden';");         this.response.write(@"window.clearinterval();");         this.response.write(@"}");         this.response.write(@"startshowwait();");         this.response.write(@"</script>");          this.response.flush();         thread.sleep(500);     }      private datatable getdata(sqlcommand cmd)     {         datatable dt = new datatable();         string strconnstring = system.configuration.configurationmanager.              connectionstrings["conn"].connectionstring;         sqlconnection con = new sqlconnection(strconnstring);         sqldataadapter sda = new sqldataadapter();         cmd.commandtype = commandtype.text;         cmd.connection = con;         try         {             con.open();             sda.selectcommand = cmd;             sda.fill(dt);             return dt;         }         catch (exception ex)         {             throw ex;         }                 {             con.close();             sda.dispose();             con.dispose();         }     }      public override void verifyrenderinginserverform(control control)     {     } }    

yeah, can't that. in pleasewait method, you're calling response.flush. sends buffered output, along http headers. http headers sent before other response data, you're out of luck.

the usual way have separate pleasewait page, timed redirect actual file (which sends file data along proper headers).

the basic issue http "single request, single response". you're trying return 2 responses single request, , not possible on pure http (sadly; there http-like protocols allow this, aren't supported in browsers - it's great boost performance when can send 20 files @ once, rather in 20 separate requests).


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -