0

Render像这样压倒一切:

protected override void Render(HtmlTextWriter writer)
{
    var keyCheck = HttpContext.Current.Session["PaymentSearchResults"];

    if (_exportToExcel && keyCheck != null)
    {
        ExportToExcel();
        return;
    }

    base.Render(writer);
}

要下载我在ExportToExcel方法中添加到响应中的 Excel 文件,如下所示:

File.WriteAllBytes(tempFileName, buffer);

Response.ContentType = "application/ms-excel";
Response.WriteFile(tempFileName);

HttpContext.Current.ApplicationInstance.CompleteRequest();

但是,响应永远不会返回到浏览器,因此浏览器只是挂起。

我究竟做错了什么?

4

1 回答 1

2

这就是我在按钮单击事件中使用的。奇迹般有效。

HttpContext httpCtx = System.Web.HttpContext.Current;

httpCtx.Response.Clear();
httpCtx.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
httpCtx.Response.ContentType = "application/vnd.ms-excel";
httpCtx.Response.Charset = "utf-8";

httpCtx.Response.WriteFile(filePath);

httpCtx.Response.End();
于 2012-12-03T18:53:08.517 回答