输出CSV以通过jquery下载

时间:2010-09-06 11:05:52

标签: javascript jquery csv

更新:

解决途径:

            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+''});
        } 

想法?

1 个答案:

答案 0 :(得分:1)

我不认为这是可能的,因为所有标头都已发送。正如Pointy所说的那样,把它分成两部分,一部分回答ping,即传递文件的那部分。