下载文件中出现"Server cannot append header after HTTP headers have been sent."错误



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

在 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 中遇到错误

受保护的无效Application_Error(对象发送器,事件参数 e)"发送 HTTP 标头后,服务器无法附加标头。"

谢谢。

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

相关内容

最新更新