下载文件时,加载图标会一直显示

时间:2015-03-24 08:02:55

标签: php jquery pdf download header

下载pdf文件时,加载图标会一直显示在页面中。似乎beforeunload函数(显示加载图标)在下载文件时被触发,但页面未被卸载。我的代码中会出现什么错误?

<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>

0 个答案:

没有答案