Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When downloading a pdf file, loading icon keeps on displaying in the page. It seems like beforeunload function is getting triggered when downloading a file. What would be the mistake in my code?

t1.php
HTML
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>
<script>
        $(window).on('beforeunload', function(event) {
            $('#loading').show();
        });
        $(window).on('load', function(event) {
            $('#loading').hide();
        });
 </script>  
 <div id="loading">Loading...</div>
 <a href="t1.php?pdf=<?php echo 'Intro.pdf';?>"><button>download</button></a>
    <?php
        if(isset($_GET['pdf'])){
            $bbpdf = $_GET['pdf'];
            header("Pragma: public"); // required
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private", false); // required for certain browsers
            header("Content-Type: application/pdf");
            header("Content-Disposition: attachment; filename=\"" . basename($bbpdf));
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: " . filesize($bbpdf));
            ob_clean();
            flush();
            readfile($bbpdf);
        }        
    ?>
</body>
</html>
Posted
Updated 14-Feb-18 15:16pm
v2
Comments
ramyajaya 24-Mar-15 20:45pm    
The problem I find in your code is $window.load is not getting called back . This will not called after your document is downloaded as because you are doing all the operation in the same page, load function will be called on loading of the page or refresh.

You can try some other option like display the loading div on calling of get pdf pho function and hide it off when that function complete its operations

Hope it helps
Vidhya Raju 25-Mar-15 1:46am    
I tried php program in a separate file then too same issue exists. The page doesn't unload though beforeunload function getting triggered
ramyajaya 25-Mar-15 6:44am    
if you are ready to use two php then $(window).load function is not needed.
it flows like this

Currentwindow.php-> on button click-> window.unload display loading msg-> download.php does download operation and in end it redirect to current windows.php
Vidhya Raju 25-Mar-15 6:56am    
But I need beforeunload and load function to display loading icon while redirecting into another page. Just am facing issue when downloading a file. why beforeunload function is getting triggered when I try to download file? No way to solve it?
ramyajaya 25-Mar-15 7:57am    
Instead of beforeunload try unload event since thise working of this event differs from browser to browser .

Reference : https://api.jquery.com/unload/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900