This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Close Popup Window after download completes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Close Popup Window after download completes" Watch "Close Popup Window after download completes" New topic
Author

Close Popup Window after download completes

Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Hi All,
I am placing a link on a page, which when clicked, initiates a download on the client computer. I am doing this, by calling a new popup window, and then calling a javascript function onLoad in that popup page. The javascript clickes on a hidden link and invokes a bean method which leads to the download. Now i want to be able to close the popup window after the user has downloaded/cancelled the file. I want the popup window to close automatically after the download.
How can i do this? If this is not possible, can i do some simulation so that the window closes on some logical event?


Thanks


When the compiler's not happy, ain't nobody happy.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30352
    
150

I believe you can't know when the download is complete. What is the point of this pop-up? Why not give them a link to download from the main window?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Hi Jeanne,

The file to be downloaded is not static, it is created after fetching a lot of data from the DB and then processing it. This takes some time, hence in order to not keep the user waiting, i am using a popup window. The user can then continue his work on the main window and navigate away from that page too, being prompted for the download after the file is ready.

Thanks.
Ayoma Wijethunga
Ranch Hand

Joined: Mar 07, 2010
Posts: 38

Only problem is identifying if the download is complete. I don't think there is a possible way of doing that. Anyway is there any use of keeping the pop-up open untill the download is complete. You can simply close the pop-up once the download is initiated. Using below JavaScript, can't you?



AyomaOnline - CodeGreed
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Hi Ayoma,

Will this not lead to confusion to the user? I mean, as i said, the creation of the file to be downloaded can take anywhere between 2-10 minutes, so if we close the popup window as soon as the link is created, the user has to simply wait and watch. Also, i cannot understand how the file will be sent if the popup is closed, the data is written to the response stream of the wndow, right?

Thanks.
Ayoma Wijethunga
Ranch Hand

Joined: Mar 07, 2010
Posts: 38

Yes.. That will not work, if generation of the download is taking that much of time. So the generation begins with the opening of pop-up.

What do you think about this approach:

  • Generation begins with the opening of pop-up
  • Progress of generation process is stored in a database or somewhere
  • AJAX request sent by pop-up, checks the progress of generation, for each 10 seconds or so
  • Progress of the generation process will be shown in pop-up. So user knows that everything is working fine.
  • Pop-up will close itself after the generation process was complete (where user will be prompt for the download)


  • This is the only solution I can see at the moment.

    Regards
    Ayoma Wijethunga
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 38

    Wait.. Another problem. How were you planing to prompt user for the download once the generation is over? Did you use a AJAX request or???
    Rajkamal Pillai
    Ranch Hand

    Joined: Mar 02, 2005
    Posts: 443
        
        1

    Hi,

    I remember doing somthing similar. I popup the new window containing data to be printed call a window.print() using javascript and then call a window.close(). This met my requirement. To my knowledge the window.print() would send the document to the print queue and even if the window closes before the actual print operation it would not be affected.

    Cheers,
    Raj.
    Janardan Kelkar
    Ranch Hand

    Joined: May 05, 2008
    Posts: 72

    Hi All,

    @Ayoma

    Once the file is created, it is written to the servletoutput stream. Here is the method that does this-

    This leads to the browser popup which prompts the user to open/save/cancel the file.

    @Raj

    I did not understand your concept, could you elaborate a little more?
    The file is not to be displayed in the window, it should be donwloadable to the local machine.

    Thanks All.
    Ayoma Wijethunga
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 38

    OK! Now it is much clear.

    Try this out. After calling "response.flushBuffer();" method it can be considered that the generation and prompt for download is complete. So user already has the download or "save as" windows opened, making it simply possible to close the popup without affecting rest of the operation.

    Add below line after "response.flushBuffer();"


    Here is the code for "closeWindow.htm"


    Note that "window.close()" method can only be used with pop-ups or winnows opened with "window.open()" method.
    Write back if this fail.

    Regards,

    PS - Raj was talking about the window.print() method used to take a printout of a page using a Printer. So I don't think that method will meet your requirements.
    Janardan Kelkar
    Ranch Hand

    Joined: May 05, 2008
    Posts: 72

    Hi Ayoma,
    I Tried this method. The popup window closes, but before the download popup appears to the user. I tried to put delays before "response.sendRedirect()", but still it seems that the sendRedirect method does not let the download popup to appear before it executes.!!

    Thanks.
    Ayoma Wijethunga
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 38

    Now, we only have to place the response.sendRedirect() in the correct place. Did you try placing it after "facesContext.responseComplete();" .

    If this won't work, we'll have to take a deep look into the code. Some debugging will help to identify the location yourself.

    Regards.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Close Popup Window after download completes