James123
James123

Reputation: 11652

Close window after Response.End()?

I am using the code below to download an Excel file. Once the Response.End() call runs, I want to close window. However, this isn't happening. Please see the code I have so far below.

'Write it back to the client

   Dim filename As String = "FullExtract_" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & ".xlsx"
   Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   Response.AddHeader("content-disposition", "attachment;  filename=" & filename)
   Response.BinaryWrite(pck.GetAsByteArray())
   Response.End()
   'cursor not reaching to below code   
   Page.ClientScript.RegisterStartupScript(Me.GetType(), "closedownload", "JavaScript:window.close(); return false;")

How do I close the window?

Upvotes: 3

Views: 14331

Answers (4)

Aran Tamool
Aran Tamool

Reputation: 309

I had a similar issue, please check my answer below and I hope it will help you.

var popupWindow = window.open("endpoint","","setting");
var att = document.createAttribute("onblure");  // Create a "onblure" attribute
att.value = "window.close()";                  // Set the value of the class attribute
popupWindow.setAttributeNode(att); 

This will allow closing the window when the download window appears

Note: endpoint: is your new window endpoint setting: is your window setting for example width, height,top, left .. etc

Upvotes: 1

shenhengbin
shenhengbin

Reputation: 4294

First , I think your Close Window Script should be like this ↓

Page.ClientScript.RegisterStartupScript(this.GetType(), "closedownload", "<script language='javascript'>window.opener=null;window.close();</script>");

Then ,I think why you can't close window after response.end() is about the ASP.NET LIFE CYCLE

From the above document , We can learn the order

Page Load -> Control Event -> Page Render .

1, The javascript you have registered will excute when your page has been rendered.

2, when you excute the Response.End() ( which would absolutely stop the page-lifecycle execution) in the event , the page render event would never be fired.

3, That's why you can't close your window after Response.End(). IMHO

Upvotes: 0

Andy
Andy

Reputation: 8562

Don't call Response.End. That throws a ThreadAbortException, which will cause all kinds of nasty things. Also, this exception is what is preventing your Page.ClientScript after the Response.End not to fire. So just remove the Response.End. Its no longer needed and a hold over from the old classic ASP days.

EDIT: You can't close the window like that; when you send the file for download, it should open a file save prompt on the users OS, and the file will download. The html RegisterStartupScript would generate would end up as part of the file download I'd think. You should probably have the page which the button or link that triggers the download open a new window using target="_blank" or via javascript, that window would close automatically or not even opene depending on the browser.

Upvotes: 1

Lukasz M
Lukasz M

Reputation: 5723

Calling Response.End() stops execution of the page, so the line that registers the script is just not processed. However, I suppose that Response.End() should be called after sending file contents to the browser in order for it to receive the file correctly.

In order to close the window, but allow the file to be downloaded, you can try to use java script to show a popup window and then close it, while leaving its parent page opened.

There are some suggestions for solution of a similar problem here:

Upvotes: 0

Related Questions