【问题标题】:Getting "Server cannot append header after HTTP headers have been sent."error in download file获取“发送 HTTP 标头后服务器无法附加标头。”下载文件中的错误
【发布时间】:2016-02-09 06:13:59
【问题描述】:

我在登录页面下载文件后得到。

在 JS 中,

$('#DownloadForm').on('click', function (event) {       
  window.location = '/Form/Form1'
});

在控制器中,

public void Form1()
{
    string path = Server.MapPath("~/Form/Form.xls")
    FileStream objFileStream = System.IO.File.OpenRead(path);
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("content-disposition", "attachment; filename=" + "Form.xls");
    Response.AppendHeader("content-length", objFileStream.Length.ToString());
    int nSize = 0;
    byte[] arrBuffer = new byte[1024 * 215];
    while ((nSize = objFileStream.Read(arrBuffer, 0, arrBuffer.Length)) > 0)
    {
        Response.OutputStream.Write(arrBuffer, 0, nSize);
        Response.Flush();
    }
    Response.End();
}

我在 Global.asax 中遇到错误

protected void Application_Error(object sender, EventArgs e) “发送 HTTP 标头后,服务器无法附加标头。”

谢谢。

【问题讨论】:

    标签: model-view-controller


    【解决方案1】:

    我认为你需要从 Response.Clear(); 开始

    【讨论】:

    • 您好,感谢您的回复。我测试了一下,仍然出现错误。如果我从其他页面调用该函数,我不会收到错误,如果我从登录页面调用,我会收到错误。
    猜你喜欢
    • 2011-09-13
    • 2011-02-21
    • 2023-03-08
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多