我正在尝试设置一个StreamedResponse,以便导出一个csv文件。但是,除了静态数组之外,我没有其他东西可以进入流中。
此功能不起作用
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
// THIS LOOP BREAKS THE RESPONSE
foreach ($tests as $test) {
fputcsv($handle, $test, ',');
}
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}但是如果我去掉了前程循环,它就能工作了。
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}调用count()函数也会破坏它
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
// THIS COUNT BREAKS THE RESPONSE
$i = count($tests);
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}编辑:
如果我尝试fputcsv其中一个$tests“行”,它也会中断。
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
// THIS FPUTCSV BREAKS THE RESPONSE
fputcsv($handle, $test[0], ',');
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}但是,如果数组是内联的,我可以多次放置fputcsv。
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}在不工作的情况下,将铬重定向到我操作的路由,并显示一条"ERR_INVALID_RESPONSE“消息
我如何使用StreamedResponse来做一些有用的事情?
发布于 2016-02-24 00:09:57
我在这里看到了我错过的地方,我忘记使用回调函数之外的变量()
public function exampleCsvAction(Request $request)
{
$tests = array(
array('one', 'two', 'three'),
array('one', 'two', 'three'),
array('one', 'two', 'three')
);
$response = new StreamedResponse();
$response->setCallback(function () use(&$tests) {
$handle = fopen('php://output', 'w+');
fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
foreach ($tests as $test) {
fputcsv($handle, $test, ',');
}
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
return $response;
}https://stackoverflow.com/questions/35590613
复制相似问题