发送HTTP标头后,服务器无法设置内容类型

时间:2012-02-20 12:07:16

标签: c# asp.net sharepoint-2010

我收到错误(服务器无法在发送HTTP标头后设置内容类型。)在以下代码(ContentType行)上。我应该改变什么?

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(PervasiveConstants.DownloadZipLocation) + ";");
response.TransmitFile(PervasiveConstants.DownloadZipLocation);
response.Flush();
response.End();  

这是在Sharepoint 2010网站部分内。

6 个答案:

答案 0 :(得分:6)

尝试设置response.BufferOutput = true;。设置response变量后立即执行此操作。

答案 1 :(得分:3)

答案 2 :(得分:1)

我在网络表单上遇到了类似的问题。我通过在后面的代码中将以下代码添加到我的按钮来解决了这个问题:

ScriptManager.GetCurrent(this).RegisterPostBackControl(btnPrint);

答案 3 :(得分:1)

需要设置内容类型吗?此解决方案不容忽视,因为您可能会发现代码可以完美地工作,而无需为正在发送的响应类型明确定义内容类型。因此,删除以下行应该可以使错误消失,并且很可能在不引入新问题的情况下做到这一点(当然,您需要针对自己的情况进行测试):

response.ContentType = "text/plain";

答案 4 :(得分:0)

此解决方案对我有用, 在方法的开头添加这行代码,

Server.ClearError();

答案 5 :(得分:-2)

我遇到了同样的问题,以下解决方案解决了这个问题, 在运行response.ClearHeaders();

之前运行response.AddHeader(