获取"服务器无法在发送HTTP标头后附加标头。"下载文件中出错

时间:2016-02-09 06:13:59

标签: model-view-controller

我在登录页面中获取下载文件。

在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标头后追加标头。"

感谢。

1 个答案:

答案 0 :(得分:0)

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

开始