I am using ASP.NET and C#. I am generating pdf and sending that on the pageload using
我正在使用ASP.NET和C#。我正在生成pdf并使用在页面加载上发送
response.TransmitFile(file);
So after this I need to close this window.I try to write the dynamic script for closing, but that did not work.
所以在此之后我需要关闭这个窗口。我尝试编写动态脚本来关闭,但这不起作用。
On button click i am using this code to open the window.
在按钮上单击我使用此代码打开窗口。
window.open("Export.aspx?JobNumbers=" + jobnums,'',"resizable=0,scrollbars=0,status=0,height=200,width=500");
On pageload of export.cs I am creating the pdf using itextsharp.Then snding that using this.It is called on the buttonclick of the button that is clicked dynamically using
在export.cs的页面加载中,我使用itextsharp创建pdf。然后使用this进行snd。在按钮上单击按钮,调用它。
string script = "var btn = document.getElementById('" + Button1.ClientID + "');";
script += "btn.click();";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Eport", script, true);
This is the onclick event of button.
这是按钮的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();
}
After this i need to close this export.aspx window for that i used this.
在此之后,我需要关闭此export.aspx窗口,因为我使用了这个。
Page.ClientScript.RegisterStartupScript(this.GetType(), "Export", "window.onfocus=function(){window.close();}", true);
And
和
HttpContext.Current.Response.Write("<script>window.onfocus=function(){window.close();}</script>");
But did not worked.
但没有奏效。
Is it possible?
可能吗?
4 个解决方案
#1
1
you should try something like this:
你应该尝试这样的事情:
<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>
This will try to close the window after 5 seconds, leaving enough time for the download to start.
这将在5秒后尝试关闭窗口,留下足够的时间开始下载。
You will have to put this in a page, open that page in a popup window, so this script will execute, request the file, and close itself.
您必须将它放在一个页面中,在弹出窗口中打开该页面,这样该脚本将执行,请求文件并关闭它自己。
#2
1
on the body tag html you must set event unload="window.close()" . This worked for me
在body标签html上你必须设置event unload =“window.close()”。这对我有用
#3
0
I'd suggest to write an HttpHandler:
我建议写一个HttpHandler:
/// <summary>
/// here is your server-side file generator
/// </summary>
public class export : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// get some id from query string
string someid = context.Request.QueryString["id"];
try
{
...
context.Response.ContentType = "application/pdf";
context.Response.HeaderEncoding = System.Text.Encoding.UTF8;
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=filename.pdf");
context.Response.Write(yourPDF);
/* or context.Response.WriteFile(...); */
}
catch (Exception ex)
{
/* exception handling */
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
do not forget to register it in the Web.config:
不要忘记在Web.config中注册它:
<system.webServer>
<handlers>
<add name="/export_GET" path="/export" verb="GET" type="YourNamespace.export" preCondition="integratedMode,runtimeVersionv4.0" resourceType="Unspecified" />
</handlers>
</system.webServer>
after that you just have to call to this handler from javascript like this:
之后你只需要从这样的javascript调用这个处理程序:
ExportFile: function () {
...
var url = "http://" + window.location.hostname + ":4433/export?cid=" + id;
window.open(url);
}
it will show you save dialog without needing to manually close a new window (it will close automatically).
它将显示您保存对话框而无需手动关闭新窗口(它将自动关闭)。
#4
-1
Since it is obvious that you can't close window that is created by browser as result of downloading a file (like list of downloaded files/do you want to open prompts) I assume the question is "how to close hosting page that initialted "get PDF" operation.
因为很明显你不能关闭浏览器创建的窗口,因为下载文件(如下载文件列表/你想打开提示)我假设问题是“如何关闭初始化的主机页面”得到PDF“操作。
Since there is no way for page to know about completion of downloading of a file you need may need to ask server (i.e. AJAX erquest) if dowload is complete - it will require some server side code for tracking downloads (and you may not be able to use session state for storing download status as it will be locked for other requests). When you got confirmation that download is complete you can close the window. Note that closing window that was opened by the user will either fail or at least show confifmation "Do you really want to close"...
由于页面无法知道文件下载的完成情况,您可能需要询问服务器(即AJAX erquest),如果下载完成 - 它将需要一些服务器端代码来跟踪下载(并且您可能无法使用会话状态来存储下载状态,因为它将被锁定以用于其他请求)。当您确认下载完成后,您可以关闭窗口。请注意,用户打开的关闭窗口要么失败,要么至少显示出“你真的想要关闭”的信息......
#1
1
you should try something like this:
你应该尝试这样的事情:
<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>
This will try to close the window after 5 seconds, leaving enough time for the download to start.
这将在5秒后尝试关闭窗口,留下足够的时间开始下载。
You will have to put this in a page, open that page in a popup window, so this script will execute, request the file, and close itself.
您必须将它放在一个页面中,在弹出窗口中打开该页面,这样该脚本将执行,请求文件并关闭它自己。
#2
1
on the body tag html you must set event unload="window.close()" . This worked for me
在body标签html上你必须设置event unload =“window.close()”。这对我有用
#3
0
I'd suggest to write an HttpHandler:
我建议写一个HttpHandler:
/// <summary>
/// here is your server-side file generator
/// </summary>
public class export : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// get some id from query string
string someid = context.Request.QueryString["id"];
try
{
...
context.Response.ContentType = "application/pdf";
context.Response.HeaderEncoding = System.Text.Encoding.UTF8;
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=filename.pdf");
context.Response.Write(yourPDF);
/* or context.Response.WriteFile(...); */
}
catch (Exception ex)
{
/* exception handling */
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
do not forget to register it in the Web.config:
不要忘记在Web.config中注册它:
<system.webServer>
<handlers>
<add name="/export_GET" path="/export" verb="GET" type="YourNamespace.export" preCondition="integratedMode,runtimeVersionv4.0" resourceType="Unspecified" />
</handlers>
</system.webServer>
after that you just have to call to this handler from javascript like this:
之后你只需要从这样的javascript调用这个处理程序:
ExportFile: function () {
...
var url = "http://" + window.location.hostname + ":4433/export?cid=" + id;
window.open(url);
}
it will show you save dialog without needing to manually close a new window (it will close automatically).
它将显示您保存对话框而无需手动关闭新窗口(它将自动关闭)。
#4
-1
Since it is obvious that you can't close window that is created by browser as result of downloading a file (like list of downloaded files/do you want to open prompts) I assume the question is "how to close hosting page that initialted "get PDF" operation.
因为很明显你不能关闭浏览器创建的窗口,因为下载文件(如下载文件列表/你想打开提示)我假设问题是“如何关闭初始化的主机页面”得到PDF“操作。
Since there is no way for page to know about completion of downloading of a file you need may need to ask server (i.e. AJAX erquest) if dowload is complete - it will require some server side code for tracking downloads (and you may not be able to use session state for storing download status as it will be locked for other requests). When you got confirmation that download is complete you can close the window. Note that closing window that was opened by the user will either fail or at least show confifmation "Do you really want to close"...
由于页面无法知道文件下载的完成情况,您可能需要询问服务器(即AJAX erquest),如果下载完成 - 它将需要一些服务器端代码来跟踪下载(并且您可能无法使用会话状态来存储下载状态,因为它将被锁定以用于其他请求)。当您确认下载完成后,您可以关闭窗口。请注意,用户打开的关闭窗口要么失败,要么至少显示出“你真的想要关闭”的信息......