栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用“ AJAX”下载CSV文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用“ AJAX”下载CSV文件

如果要强制下载,则可以将当前页面重定向到下载链接。由于链接将生成一个下载对话框,因此当前页面(及其状态)将保留在原位。

基本方法:

$('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
,而是将副本保存在缓存中。我对此不太确定,但看起来很有希望。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号