1

I have a very particular scenario where user can download a file from server by clicking on a button on the web page. I am achieving this through Response object. This is working fine.

But now I want to close the web page once the download completes. I have already tried the below code.

protected void btnDownloadFM_Click(object sender, EventArgs e)
{
        bool isSucced = DownloadFile();

        if (isSucced)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
        }
}

The above code is not working. But if I comment out the file download code it is working fine(the web page close properly).

protected void btnDownloadFM_Click(object sender, EventArgs e)
{
            //bool isSucced = DownloadFM();
            bool isSucced = true;

            if (isSucced)
            {
                ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
            }
}

Below is the code block for downloading the file.

private bool DownloadFM()
{
    try
    {
        //Get the file byte array from DB.
        byte[] bytes = GetFileBytesFromDB();
        string fileName = "DownloadedFile.txt";

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();

        string contentDisposition;
        if (Request.Browser.Browser == "IE")
            contentDisposition = "attachment; filename=" + Uri.EscapeDataString(templateName);
        else
        {
            contentDisposition = "attachment; filename*=UTF-8''" + Uri.EscapeDataString(templateName);
        }
        Response.AddHeader("Content-Disposition", contentDisposition);
        Response.AddHeader("Content-Length", bytes.Length.ToString());
        Response.AddHeader("X-Download-Options", "noopen");
        Response.ContentType = "application/octet-stream";
        Response.OutputStream.Write(bytes, 0, bytes.Length);
        Response.Flush();
        Response.Close();
        return true;
    }
    catch (Exception ex)
    {
        //Log error message to DB
        return false;
    }
}

Am I doing anything wrong here?

Amit
  • 623
  • 2
  • 8
  • 18
  • 1
    Can you show us DownloadFile() code? Does it returns true when uncommented? – Microsoft DN Jun 30 '15 at 11:59
  • Once the Response stream has been read the Response ends and registering a startup script will have no effect (it is essentially like altering the markup after the page is finished rendering). – Kevin Jun 30 '15 at 12:04
  • @MicrosoftDN Updated the question with the code block. – Amit Jun 30 '15 at 12:08
  • @Kevin, I am afraid the same(even though I am not clear). If that is true then is there any alternative? – Amit Jun 30 '15 at 12:10
  • You do not have any return statement in your DownloadFile() ??? – Microsoft DN Jun 30 '15 at 12:12
  • If by "close a web page" you mean close the browser (which is what the script you are trying to execute should do), then I don't think so. Once the Response ends you are done processing until the browser makes a new Request. And that would require interaction by the user. – Kevin Jun 30 '15 at 12:15
  • @Microsoft DN even if he did, it would never execute, once he calls Response.Close the Response has finished and all processing on that page is done. – Kevin Jun 30 '15 at 12:19
  • @MicrosoftDN My bad. I updated the code block. – Amit Jun 30 '15 at 12:19
  • Regarding Response.Close(), this seems like the wrong context to be using it. See here: http://stackoverflow.com/questions/3219701/asp-net-response-close-issue – sr28 Jun 30 '15 at 12:39

2 Answers2

0

after flushing file you have to return and no need to close window after flushing content it's close automatically

protected void btnDownloadFM_Click(object sender, EventArgs e)
{
        DownloadFile();
        Return;
 }

try this hope it helps you

Nemi Chand
  • 138
  • 1
  • 10
0

Try registering window close before close (Not tested)
Just found similar solution here closing a window after Response.End

....
....

ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
Response.Flush();
Response.Close();

OR

Response.Clear();
Response.Write("<script>window.close();</script>");
Response.Flush();
Response.End();
Community
  • 1
  • 1
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71