c# - 响应后如何关闭窗口?

标签 c# javascript jquery asp.net

我正在使用 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/

相关文章:

javascript - 在获取更新时将 CSS 类添加到 React DOM 元素

javascript - 如何防止人们掉落在不可掉落元素中?

javascript - 使用 jQuery 选择特定的 contenteditable div

c# - 为什么在使用静态导入时不能将扩展方法作为静态方法调用?

c# - SQLite 更新后 : Unable to find an entry point named 'sqlite3_changes_interop' in DLL 'SQLite.Interop.dll'

c# - Azure SSO OnTokenValidated 不起作用

jQuery 按跨度对列表进行排序

c# - 使用 MVC Razor View 发送电子邮件

javascript - 如何使用 ExtendScript 在 After Effects 中创建文本框?

javascript - 将外部页面源存储在 Javascript 变量中?