如果要强制下载,则可以将当前页面重定向到下载链接。由于链接将生成一个下载对话框,因此当前页面(及其状态)将保留在原位。
基本方法:
$('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-downloadif(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { header("Content-Disposition: attachment; filename=search_results.csv");}////Generate csv//echo $csvOutputexit();两个请求的URL必须相同,以欺骗浏览器不要在处开始新的下载
document.location.href,而是将副本保存在缓存中。我对此不太确定,但看起来很有希望。



