php ajax csv,php – 使用“AJAX”下载CSV文件
如果您强制下载,则可以将当前页面重定向到下载链接。由于该链接将生成一个下载对话框,因此当前页面(及其状态)将保留到位。基本方法:$('a#query_name').click(function(){$('#wait-animation').show();document.location.href = '/php_scripts/utils/csv_export.php?query_name='+
如果您强制下载,则可以将当前页面重定向到下载链接。由于该链接将生成一个下载对话框,因此当前页面(及其状态)将保留到位。
基本方法:
$('a#query_name').click(function(){
$('#wait-animation').show();
document.location.href = '/php_scripts/utils/csv_export.php?query_name='+query_name;
$('#wait-animation').hide();
});
更复杂:
$('a#query_name').click(function(){
MyTimestamp = new Date().getTime(); // Meant to be global var
$('#wait-animation').show();
$.get('/php_scripts/utils/csv_export.php','timestamp='+MyTimestamp+'&query_name='query_name,function(){
document.location.href = '/php_scripts/utils/csv_export.php?timestamp='+MyTimestamp+'&query_name='+query_name;
$('#wait-animation').hide();
});
});
在PHP脚本:
@header("Last-Modified: " . @gmdate("D, d M Y H:i:s",$_GET['timestamp']) . " GMT");
@header("Content-type: text/x-csv");
// If the file is NOT requested via AJAX, force-download
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
header("Content-Disposition: attachment; filename=search_results.csv");
}
//
//Generate csv
//
echo $csvOutput
exit();
两个请求的URL必须相同,才能诱使浏览器不要在document.location.href启动新的下载,而是将副本保存在缓存中。我不完全确定,但似乎很有希望。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)