Click here to Skip to main content
13,557,707 members
Rate this:
 
Please Sign up or sign in to vote.
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>
<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 23-Mar-15 23:42pm
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
User-10967908 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
User-10967908 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/
User-10967908 25-Mar-15 8:14am
   
unload event is not working as it was deprecated. Only beforeunload works.
ramyajaya 25-Mar-15 13:36pm
   
Try to use this
$(window).on('unload', function(event) {
$('#loading').show();
});

Instead of beforeunload

This shows the msg when you navigate out of page not on any event occurrence within the page so that your get method will be called first
pankaj-nikam 14-Feb-18 21:17pm
   
Hi there,

Were you able to solve this issue? I am having the same issue.

Thanks.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 14 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100