Close Popup Window On File Download Prompt

Wee Bubba

Newcomer
Joined
Jan 27, 2005
Messages
7
i am streaming an Excel file within a popup browser window. i am trying in vain to close the popup window once the "Download File" prompt has been sent. i tried adding a Javascript line to the end of my method to achieve this, but it doesnt work. I suspect because I have cleared the Response object.

can anyone help me achieve this annoying hurdle? thanks. my code is below.

private void ExportToExcel()
{
Response.Clear();
Response.Charset = String.Empty;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + rb.ReportTitle + ".xls");
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
phBody.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
Response.Write("<script language='javascript'> { window.close() } /script>");
}
 
due to security restriction close method only works if it is opened by javascript using open method, else it will prompt to close the window

you can try using self.close

http://www.javascript-coder.com/window-popup/javascript-window-close.phtml

i tried doing the same thing with the response.write but it somehow fails, one workaround is to load something like an image and add an attribute to it "onBlur" to trigger your javascript
 
name the form in the page from where the page originates frmMain

When you invoke close, use this script

Response.Write("<script>window.opener.frmMain.submit();window.close()</script>")

This also invokes a postback in the parent form, so if you need to be able to read values from the primary form (like success or failure) you can pick them up in Page.IsPostBack
 
i dont think itll work, since you did it with response.write, the script was written first before the form was rendered to the page, window.opener.frmMain has no properties to deal with
 
I also had the same problem.

even if you open the window using window.open, it wont work.

Finally I moved the code to the parent page itself.

1. crate th binary array from the file. Pass the same to the parent page using global variable or my any other means and use response.binarywrite on the parent page instead of pop up after closing the pop up page.



Ashutosh
 
Back
Top