输出 CSV 以通过 jquery 下载

发布于 2024-09-18 00:59:38 字数 2378 浏览 4 评论 0原文

更新:

通过以下方式解决:

            append = append+'/output/'+output;
            var url = '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+'';
            $.ajax({ 
                url: ''+url+'',  
                success: function(msg) {
                    location.href = ''+url+'';
                }
            });

你好,

我有一段 Javascript 可以 ping URL 并更新表。我现在想输出它的 CSV 文件。

这样我的 PHP 端就完成了,如果我查看 ajax ping 的 URL,文件就会创建并正确输出。

但是,如何让 jQuery/Javascript 将检索到的内容输出为下载内容?

PHP:

public function outputCSV($main, $fields) {

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/excel');
    header('Content-Disposition: attachment; filename="ProducerAreaOutput-'.date('r').'.csv"');

    $array      = array();//make a new array, put the fields first
    $array[0]   = $fields;

    foreach ($main as $item) {//loop existing rows
        $array[] = $item;
    }

    $csv = self::array_to_csv($array, false);

    echo $csv;
    exit;
}

Javascript 的

    function byMonth(output) {

        var undefined;
        oTable = $('#stats').dataTable();
        oTable.fnClearTable();

        var start       = $('#year-start').val()+'-'+$('#month-start').val()+'-1';
        var end         = $('#year-end').val()+'-'+$('#month-end').val()+'-30';
        var empties     = $('#empties').val();
        var period      = $('#period').val();
        var movies      = $('#movies').val();
        var site_id     = $('#site_id').val();
        var studio_id   = $('#studio_id').val();
        var movie_id    = $('#movie_id').val();
        var append      = '';


        append = (site_id > 0)      ? append+'/site_id/'+site_id        : append;
        append = (studio_id > 0)    ? append+'/studio_id/'+studio_id    : append;
        append = (movie_id > 0)     ? append+'/movie_id/'+movie_id      : append;


        if (output === 'CSV') {
            append = append+'/output/'+output;
            $.ajax({ url: '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+''});
        } 

想法?

UPDATE:

Solved via:

            append = append+'/output/'+output;
            var url = '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+'';
            $.ajax({ 
                url: ''+url+'',  
                success: function(msg) {
                    location.href = ''+url+'';
                }
            });

Hello,

I have a piece of Javascript that pings a URL and updates a table. I now want to output a CSV file of that.

So my PHP side is done, the file is created and output correctly if I view the URL the ajax pings.

However, how do I get jQuery/Javascript to output the content its retrieved as a download?

PHP:

public function outputCSV($main, $fields) {

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/excel');
    header('Content-Disposition: attachment; filename="ProducerAreaOutput-'.date('r').'.csv"');

    $array      = array();//make a new array, put the fields first
    $array[0]   = $fields;

    foreach ($main as $item) {//loop existing rows
        $array[] = $item;
    }

    $csv = self::array_to_csv($array, false);

    echo $csv;
    exit;
}

The Javascript

    function byMonth(output) {

        var undefined;
        oTable = $('#stats').dataTable();
        oTable.fnClearTable();

        var start       = $('#year-start').val()+'-'+$('#month-start').val()+'-1';
        var end         = $('#year-end').val()+'-'+$('#month-end').val()+'-30';
        var empties     = $('#empties').val();
        var period      = $('#period').val();
        var movies      = $('#movies').val();
        var site_id     = $('#site_id').val();
        var studio_id   = $('#studio_id').val();
        var movie_id    = $('#movie_id').val();
        var append      = '';


        append = (site_id > 0)      ? append+'/site_id/'+site_id        : append;
        append = (studio_id > 0)    ? append+'/studio_id/'+studio_id    : append;
        append = (movie_id > 0)     ? append+'/movie_id/'+movie_id      : append;


        if (output === 'CSV') {
            append = append+'/output/'+output;
            $.ajax({ url: '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+''});
        } 

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-09-25 00:59:39

我认为这是不可能的,因为所有标头都已发送。按照 Pointy 的说法,将其分为两部分,一部分应答 ping,另一部分传送文件。

I don't think that is possible because all the headers are already sent. Do as Pointy says, divide this into two parts, one which answer the ping the the one which delivers the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文