jQuery文件下载+接收csv

时间:2014-04-30 10:10:06

标签: javascript php jquery twig silex

我尝试使用jquery库jquery file download创建一个excel文件。这就是我现在所拥有的:

var data = {{ tableresults|json_encode|raw }};

$('#exporttocsv').click(function(){
    $.fileDownload('dashboard/exporttocsv/' + data, {
        successCallback: function (url) {

            alert('You just got a file download dialog or ribbon for this URL :' + url);
        },
        failCallback: function (html, url) {

            alert('Your file download just failed for this URL:' + url + '\r\n' +
                    'Here was the resulting error HTML: \r\n' + html
            );
        }
    });
});

在我的功能中我有这个:

public function exporttocsvAction(Application $app, Request $request)
{
    $data = $request->request->get('data');

    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    $output = fopen("php://output", "w");

    foreach ($data as $row)
    {
        fputcsv($output, $row, ',');
    }

    fclose($output);
    exit();
}

但是当我在我的函数中转储$ data变量时,我只得到一个这样的字符串:

  

string(271)&#34; [object object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]&#34; < / p>

如何确保在php函数中获取数组?我还试过没有json_encode和raw的{{tableresults}},但这会产生语法错误:

var data = <br />

更新

这是我发送到我视图的数组的转储:http://pastebin.com/Y58aLqBX

0 个答案:

没有答案