Php 在jquery中下载pdf文件

Php 在jquery中下载pdf文件,php,jquery,pdf,download,Php,Jquery,Pdf,Download,通过jquery中的post方法,我试图下载一个pdf,但它不起作用。未下载文件。我哪里做错了 main.php <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> </head> <body> <script>

通过jquery中的
post
方法,我试图下载一个pdf,但它不起作用。未下载文件。我哪里做错了

main.php

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
        </head>
<body>
    <script>
        function downloadpdf(){
            alert("working");
        $.post('download.php',{pdf:'Intro.pdf'},function(data){if(data=="y"){alert("Downloaded");}});
    }
        </script>
    <button onclick="downloadpdf();">download</button>   
</body>
</html>

函数下载PDF(){
警惕(“工作”);
$.post('download.php',{pdf:'Intro.pdf'},函数(数据){if(数据==“y”){alert(“download”);};
}
下载
下载.php

<?php
    if(isset($_POST['pdf'])){
    $bbpdf=$_POST['pdf'];
header("Content-disposition: attachment; filename=$bbpdf");
header("Content-type: application/pdf");
header('Content-Length: ' . filesize($bbpdf));
readfile($bbpdf);
echo "y";
}
试试这个:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    </head>
    <body>
        <a href="download.php?pdf=Intro.pdf">download</a>
    </body>
</html>

<?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);
}


发生了什么,你期望发生什么?“不起作用”是一个很弱的问题描述..:未下载PFile。我想下载一个pdf文件。有错误吗?js控制台怎么说?我马上就能看到,
echo“y”
将在文件末尾添加一个
y
,我猜这不是故意的。您只想打印文件中的数据。没有错误。实际上我在download.php
if(isset($\u POST['pdf']){echo“y”}
中试过,效果很好。因此,下载pdfIt时出现
标题问题可以正常工作。那么,是否没有办法使用我发布的方法下载文件?您可以使用iframe。检查这个链接,如果它能帮助你-我有一个问题。加载图标继续显示。文件正在加载,但加载图标不会隐藏,尽管页面已完全加载。
$(窗口)。on('beforeunload',function(event){$('#load')。show();})$(window.on('load',函数(事件){$('#load').hide();})
beforeunload
在页面未卸载时触发