我正在使用 ASP.NET 和 C#。我正在生成 pdf 并使用
在页面加载上发送它response.TransmitFile(file);
因此,在此之后我需要关闭此窗口。我尝试编写用于关闭的动态脚本,但这不起作用。
单击按钮时,我使用此代码打开窗口。
window.open("Export.aspx?JobNumbers=" + jobnums,'',"resizable=0,scrollbars=0,status=0,height=200,width=500");
在export.cs的页面加载上,我使用itextsharp创建pdf。然后使用它进行snding。它在使用动态单击的按钮的buttonclick上调用
string script = "var btn = document.getElementById('" + Button1.ClientID + "');";
script += "btn.click();";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Eport", script, true);
这是按钮的onclick事件。
protected void StartExport(object sender, EventArgs e)
{
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(strFilePath));
response.TransmitFile(strFilePath);
response.Flush();
}
此后我需要关闭这个export.aspx窗口,因为我使用了它。
Page.ClientScript.RegisterStartupScript(this.GetType(), "Export", "window.onfocus=function(){window.close();}", true);
还有
HttpContext.Current.Response.Write("<script>window.onfocus=function(){window.close();}</script>");
但没有成功。
可能吗?
最佳答案
你应该尝试这样的事情:
<script>
$.get('/call/the/page.aspx'); // Send a request to the page from which you Transmit the file
window.setTimeOut(function() { window.close(); }, 5000);
</script>
这将尝试在 5 秒后关闭窗口,为开始下载留出足够的时间。
您必须将其放在一个页面中,在弹出窗口中打开该页面,以便该脚本将执行、请求文件并自行关闭。
关于c# - 响应后如何关闭窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854078/