php – 从ajax post下载pdf
发布时间:2020-12-13 17:11:55 所属栏目:PHP教程 来源:网络整理
导读:我想通过 jquery帖子下载pdf文件.我试过这个: $('#downloadPdf').click(function() { var id = $('#fileId').val(); $.ajax({ type: "POST",url: "includes/download_pdf.php",data: 'file_id=' + id,complete: function(data) { $('#pdf_results').html(da
|
我想通过
jquery帖子下载pdf文件.我试过这个:
$('#downloadPdf').click(function() {
var id = $('#fileId').val();
$.ajax({
type: "POST",url: "includes/download_pdf.php",data: 'file_id=' + id,complete: function(data) {
$('#pdf_results').html(data.responseText);
}
});
});
代码PHP: <?php
$file_name = $_POST['file_id'];
// We'll be outputting a PDF header('Content-type: application/pdf');
// It will be called downloaded.pdf header('Content-Disposition: attachment; filename="'.$file_name.'.pdf"');
// The PDF source is in original.pdf readfile('/var/www/vhosts/domain.nl/private/'.$file_name.'.pdf');
?>
当我使用这段代码时,我会在#pdf_results中看到所有奇怪的迹象.我想将PDF保存到我的电脑,但这不起作用. 有人知道如何解决这个问题? 提前致谢! 解决方法
您不需要为此使用AJAX.尝试使用这样的东西:
$(function(){
$('#downloadPdf').click(function(){
var id = $('#fileId').val();
window.open(location.protocol + '//' + location.hostname + '/includes/download_pdf.php?file_id=' + id);
return false;
});
});
干杯! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
