gpt4 book ai didi

php - 从 ajax post 下载 pdf

转载 作者:可可西里 更新时间:2023-10-31 23:08:37 25 4
gpt4 key购买 nike

我想通过 jquery post 下载 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;
});
});

干杯!

关于php - 从 ajax post 下载 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10409802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com