使用“AJAX"下载 CSV 文件; [英] Download CSV file using "AJAX"

查看:43
本文介绍了使用“AJAX"下载 CSV 文件;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站完成一项相当简单的任务,但我不确定如何去做.我希望用户查看表格,然后单击一个按钮,此时用户可以将该表的内容保存为 csv 文件.此请求有时可能非常复杂,因此我生成了一个进度页面来提醒用户.

I'm trying to accomplish a fairly simple task for my website, but I"m not sure exactly how to go about it. I want the user to be viewing a table, then click a button, at which point the user can save the contents of that table as a csv file. This request can sometimes be quite complicated so I generate a progress page to alert the user.

除了实际生成 csv 文件之外,我已经弄清楚了大多数事情.(我使用 jQuery 和 PHP)

I have most things figured out except actually generating the csv file. (I use jQuery and PHP)

点击时运行的 jQuery 代码:

the jQuery code run on click:

hmis_query_csv_export: function(query_name) {
    $.uiLock('<p>Query Loading.</p><img src="/images/loading.gif" />')
    $.get({
        url: '/php_scripts/utils/csv_export.php',
        data: {query_name: query_name},
        success: function(data) {
            $.uiUnlock();
        }
    });}

相关的PHP:

header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=search_results.csv");
//
//Generate csv
//
echo $csvOutput
exit();

这样做是将文本作为 PHP 文件发送,但不会生成下载.我做错了什么?

What this does is sends the text as the PHP file, but it's doesn't generate a download. What am I doing wrong?

推荐答案

如果您要强制下载,可以将当前页面重定向到下载链接.由于该链接将生成一个下载对话框,因此当前页面(及其状态)将保持不变.

If you are forcing a download, you can redirect the current page to the download link. Since the link will generate a download dialog, the current page (and its state) will be kept in place.

基本方法:

$('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 开始新的下载,而是将副本保存在缓存中.我不完全确定,但看起来很有希望.

The URL for both requests must be the same to trick the browser not to start a new download at document.location.href, but to save the copy at the cache. I'm not totally sure about it, but seems pretty promising.

这篇关于使用“AJAX"下载 CSV 文件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆