0

When downloading a pdf file, loading icon keeps on displaying in the page. It seems like beforeunload function (which shows loading icon) is getting triggered when downloading a file though page is not unloaded. What would be the mistake in my code?

<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>
Learning
  • 848
  • 1
  • 9
  • 32
  • The problem here is that the browser does not know that the requested file is actually a file download and not an inline page to display. – Salketer Apr 09 '15 at 07:56
  • Then what would be the solution? – Learning Apr 09 '15 at 11:50
  • I am sorry I do not have the solution. You might want to find a way to detect if the link clicked is leading to a download resource and if so do not show the loading element. – Salketer Apr 09 '15 at 12:13
  • File is getting downloaded and also loading icon keeps on displaying. – Learning Apr 09 '15 at 12:38
  • You might find something interesting in my answer here http://stackoverflow.com/a/29532789/1620194 – Salketer Apr 10 '15 at 06:12

0 Answers0