如何在Response.Close()之后调用javascript函数

How called javascript function after Response.Close()

本文关键字:之后 调用 javascript 函数 Response Close      更新时间:2023-12-03

在ASP.Net Web窗体的代码隐藏中关闭响应后,我遇到了调用javascript函数的问题。

我的代码:

string CloseJS = "<script>window.parent.SaveToSAVAction.Close();</script>";
httpContext.Response.Clear();
httpContext.Response.ContentType = "application/force-download";
httpContext.Response.AppendHeader("Content-Disposition",header);
httpContext.Response.BinaryWrite(viewModel.Create());
httpContext.Response.Flush();
httpContext.Response.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(), "closeScript", CloseJS);

也许有人知道如何修复它,或者知道下载字节数组的更好方法。

您需要在关闭响应之前调用JavaScript。

创建.aspx通用处理程序下载文件,然后你可以称之为类似的东西

ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv()", true);
Response.Redirect("MyHandler.ashx")

js函数隐藏div并重定向

function HideDivNRedirect()
{
  //hide div
window.location.href='myhandler.ashx';
}
ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDivNRedirect();", true);