Php 如何通过post回调函数触发下载提示
我猜是个简单的问题,但我想不出来。。。问题是: 在前端,我将一些用户选择的数据导出到服务器以动态创建一个文件:Php 如何通过post回调函数触发下载提示,php,jquery,post,download,Php,Jquery,Post,Download,我猜是个简单的问题,但我想不出来。。。问题是: 在前端,我将一些用户选择的数据导出到服务器以动态创建一个文件: $("#export-button").click(function() { $.post("'.$postUrl.'",{variable:exportSelection}, function(data) { console.log(data); } );}); 然后,在接收数据并在服务器上创建/保存文件后,我将在php中执行以下操作: if (f
$("#export-button").click(function() {
$.post("'.$postUrl.'",{variable:exportSelection},
function(data) {
console.log(data);
}
);});
然后,在接收数据并在服务器上创建/保存文件后,我将在php中执行以下操作:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;}
为了获得下载提示,我应该在post回调函数中做什么,而不是在控制台上打印
更新:好的-我刚刚意识到我的#导出按钮是一个锚定标签,没有href。。。现在,当我指向服务器上的文件时,问题是当我单击时,它会按照链接提示保存文件等,但它会在生成“新”版本之前到达该文件(因此每次单击时都使用以前的选择)确定-我找到了答案。我只需要禁用指向任何位置的链接,然后执行以下操作: window.location='my_file_location.doc' 。。。在回调函数内部