如何解决此错误在发送HTTP标头后,服务器无法追加标头。 [英] How to resolve this error Server cannot append header after HTTP headers have been sent.

查看:474
本文介绍了如何解决此错误在发送HTTP标头后,服务器无法追加标头。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



如何解决此错误服务器无法在发送HTTP标头后追加标头。



当我运行下载excel的第二个函数时,它获取错误。



public void ExportQRCodeGenerateImageTableData(string data)

{

string attach =attachment; filename = QRGenerateImage.xls;

Response.ClearContent();

Response.AddHeader(content-处理,附加;

Response.ContentType =application / ms-excel;

Response.Write(data);

回复.Flush();

//Response.End();

}



public void ExportAllCustomerTableData(字符串数据)

{

string attach =attachment; filename = CustomerDetails.xls;

Response.ClearContent();

Response.AddHeader(content-disposition,attach); //错误:在发送HTTP标头后,服务器无法附加标头。

Response.ContentType =application / ms-excel;

Response.Write(data);

//Response.End() ;

Response.Flush();



}



请帮忙我。



预付款



Ankit Agarwal

软件工程师

Hello,

How to resolve this error "Server cannot append header after HTTP headers have been sent".

When i run second function for download excel, its getting error.

public void ExportQRCodeGenerateImageTableData(string data)
{
string attach = "attachment;filename=QRGenerateImage.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attach);
Response.ContentType = "application/ms-excel";
Response.Write(data);
Response.Flush();
//Response.End();
}

public void ExportAllCustomerTableData(string data)
{
string attach = "attachment;filename=CustomerDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attach);// Error:Server cannot append header after HTTP headers have been sent.
Response.ContentType = "application/ms-excel";
Response.Write(data);
//Response.End();
Response.Flush();

}

Please help me.

Thanks in Advance

Ankit Agarwal
Software Engineer

推荐答案

这里的问题是 Response.Flush()方法立即发送缓冲输出以及响应头和数据。



因此,一旦你在你的第一个函数中完成了 Response.Flush()标题数据。



因此,你可以有两个按钮分别下载这两个文件,或者你可以将它们压缩在一起,然后用户可以下载该zip并查看两个文件。



你可以在这里找到这个实现:从ASP.NET应用程序创建ZIP文件 [ ^ ]
Well the issue here is Response.Flush() method sends buffered output immediately along with the response header and data.

Hence once you have done a Response.Flush() in your first function you have already send the header data.

So, either you can have two buttons to download the two files separately or you can zip them together and then user can download that zip and view both the files.

You can look for this implementation here: Create ZIP Files From An ASP.NET Application[^]


嗨......我找到了太多解决方案,但大多数都在其他页面中产生问题,因此,此解决方案不会产生任何问题并正确解决您的问题。



解决方案



Hi ... I found too much solution but most of them create problem in another pages, so, this solution will not create any problem and solve your problem correctly.

Solution

public ActionResult YouActionResultName()
        {
            Response.ClearHeaders();
            var gv = new GridView();
            gv.DataSource = db.Locations.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=LocationsWord.doc");
            Response.ContentType = "application/ms-doc";
            Response.Charset = "";
            StringWriter objStringWriter = new StringWriter();
            HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
            gv.RenderControl(objHtmlTextWriter);
            Response.Output.Write(objStringWriter.ToString());
            Response.Flush();
            Response.End();
            objStringWriter.Close();
            objHtmlTextWriter.Close();
            return Redirect("Index");
        }



最重要的事情是添加

Response.ClearHeaders();

在行动结果开始。



之后转到索引并输入此代码:



in the begin of action result.

After that go to the index and put this code:

@using (Html.BeginForm("YouActionResultName", "YourControllerName", FormMethod.Post))
                {
                    <button class="btn btn-default" type="submit"> Excel <span class="glyphicon glyphicon-export"></span></button>
                }







希望得到这个帮助,祝每个人好运。 />


问候,

Ali Mosaad Mohamed,

软件开发人员


这篇关于如何解决此错误在发送HTTP标头后,服务器无法追加标头。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆