Giri
Giri

Reputation: 941

How to close the window after response?

I am using ASP.NET and C#. I am generating pdf and sending that on the pageload using

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

            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.

           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.

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?

Upvotes: 1

Views: 7830

Answers (4)

Filipe - BR
Filipe - BR

Reputation: 11

on the body tag html you must set event unload="window.close()" . This worked for me

Upvotes: 1

Vladislav
Vladislav

Reputation: 1995

I'd suggest to write an 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:

<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:

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).

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100537

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.

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"...

Upvotes: -1

BuddhiP
BuddhiP

Reputation: 6451

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.

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.

Upvotes: 1

Related Questions