Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close browser popup page after downloading file

When user clicks on a hyperlink, i am opening a popup window which downloads a file. Post download, it shows open/save dialog box. I want to close the popup window once the download is done (when user is prompted for saving the file). I have tried window.close method, but it doesnt work as the context is not the popup window but the open/save dialog box.

like image 943
Ankit Avatar asked Jan 18 '10 10:01

Ankit


3 Answers

I think you can not control it programatically. This is browser-specific thing where some browsers allow you to tick on a check box to close the window and so on.

like image 192
Sarfraz Avatar answered Oct 20 '22 16:10

Sarfraz


you can do one thing:

after the "file writing code in your servlet or struts action use"

Step 1: call "response.flushBuffer();" Step 2: call response.sendRedirect("close.htm")

where close.htm is :

<html>


<script>


window.close();

</script>

</html>

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?

 pop_up= window.open("", "PopUpName");   
 pop_up.document.write('POPUP TEXT');      
 pop_up.close(); 
like image 42
Kumar Gaurav Avatar answered Oct 20 '22 16:10

Kumar Gaurav


Browsers have a habit of deciding for themselves whether to try to download a file or open it inside the browser window, depending on the browser used, plug-ins and server settings. It sounds like you might be opening the link in a new window, as if the browser was going to open the file rather than download it, and then the browser has opted for the download. This leaves the user with a downloaded file and a blank window that you have no control over.

To force it to download, you should be able to set the Content-Type header for the target of the link to application/force-download. How you do this will depend on your setup, and whether the file is downloaded directly (in which case it will be a server setting) or via PHP or .Net (in which case it's easy to programmatically set the header). Also make sure that the hyperlink doesn't have a target="_blank" attribute that opens the link in a new window.

like image 38
Karl B Avatar answered Oct 20 '22 16:10

Karl B