2014-03-25 4 views
4

Excel 인라인 응답을 반환하려는 경우이 문제가 있습니다.HTTP 헤더를 보낸 후에 서버에서 헤더를 추가 할 수 없습니다. C# excel inline

도움말 코드 페이지에서이 답변을 검색했으며 아직 작동하지 않는 항목을 찾을 수 없습니다.

내가 아래에서 수행하는 작업의 간단한 예를 만들었습니다. 이 문제는 항상 발생합니다. 이 라인에서

오류 :

Line 56:   Response.AddHeader("content-disposition", 

코드 뒤에이 :

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     PleaseWait(); 
     Call(); 
    } 

    protected void Call() 
    { 
     strSql = "SELECT * FROM ....... ; "; 

     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); 

     for (int i = 0; i < 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; 
     } 
     finally 
     { 
      con.Close(); 
      sda.Dispose(); 
      con.Dispose(); 
     } 
    } 

    public override void VerifyRenderingInServerForm(Control control) 
    { 
    } 
} 

답변

5

그래, 당신은 할 수 없습니다. PleaseWait 방법에서는 Response.Flush이라고합니다. HTTP 헤더와 함께 현재 버퍼 된 출력을 보냅니다. HTTP 헤더는 항상 다른 응답 데이터보다 먼저 전송되므로 운이 없다.

일반적인 방법은 별도의 PleaseWait 페이지를 사용하는 것입니다.이 페이지는 실제 파일로 시간을 재지 정하여 적절한 파일 헤더와 함께 파일 데이터 만 보냅니다.

기본적으로 HTTP는 "단일 요청, 단일 응답"입니다. 하나의 요청에 대해 두 개의 응답을 반환하려고 시도하고 있으며 단순히 순수 HTTP를 통해 가능하지 않습니다 (슬프게도 HTTP를 허용하는 프로토콜이 있지만 브라우저에서는 실제로 지원되지 않습니다. 20 개의 개별 요청 대신 20 개의 파일을 동시에 전송할 수 있습니다.

+0

도움에 감사드립니다. 실제 파일을 시간 초과 리디렉션하는 별도의 PleaseWait 페이지를 만드는 예가 있습니까? – Hamamelis

+0

@ user3370558 글쎄, "기다려주세요"페이지에서 간단히'Response.Redirect'를 할 수 있습니다. 코드를 두 페이지로 나누거나 예를 들어 결정해야합니다. 질의 문자열 인'getData.aspx'는'Please wait '를 표시하고'PleaseWait' 대신'Call'을 실행할'getData.aspx? download = 1'로 리디렉션을 할 수 있습니다. – Luaan

+0

고마워요,하지만 getData.aspx 페이지에서 동일한 문제와 같은 오류가 발생했습니다 ... HTTP 헤더가이 줄에서 전송 된 후 서버가 헤더를 추가 할 수 없습니다. 50 행 : Response.AddHeader ("content-disposition" – Hamamelis

관련 문제