正在 jquery 下载 pdf 文件
Downloading a pdf file in jquery
通过 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>
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);
}
注意:不要使用 AJAX。对于通过 AJAX.
从服务器接收到的任意数据块,没有跨浏览器的方式强制浏览器在 JavaScript 中显示另存为对话框
通过 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>
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);
}
注意:不要使用 AJAX。对于通过 AJAX.
从服务器接收到的任意数据块,没有跨浏览器的方式强制浏览器在 JavaScript 中显示另存为对话框