1

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?

4
  • 2
    Try posting the code to close the form that didn't work.
    – PeterJ
    Dec 13, 2012 at 6:20
  • I did not understand the scenario ..please make your question clear by putting some code.
    – Pranav
    Dec 13, 2012 at 6:21
  • You can only close a browser window if it was opened with window.open
    – Ian
    Dec 13, 2012 at 6:22
  • Tell me whether what ever i am trying to do is possible? I can do this by not opening the export.aspx window.but i am opening that window for showing the message to the user.
    – Giri
    Dec 13, 2012 at 6:57

4 Answers 4

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.

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.

4
  • 1
    +1 this is simplest solution (unless you need to be waiting for download to complete). Dec 13, 2012 at 6:31
  • thanks..I did write settimeout with just 1ms in that page but it did start counting after he choose wheteher to save or download.Exactly what i wanted.
    – Giri
    Dec 18, 2012 at 9:11
  • cool, glad I could help and good to know that browser behaves like that. :)
    – BuddhiP
    Dec 18, 2012 at 9:28
  • can you give me a solution which doesn't use jQuery (it seems jQuery is not enabled in the environment i am working in)
    – Sadhir
    Oct 15, 2013 at 19:59
1

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

0

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

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

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

1
  • Thanks..you are right.but the settimeout is doing the trick in my case.
    – Giri
    Dec 18, 2012 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.