1
我得到了一个CSV下载混乱。我很高兴将它保存到一个文件并提供一个链接给用户,但这似乎是从事情的错误判断likethese。Laravel 5.2流下载
来自这个答案Use Laravel to Download table as CSV我想我发现stream()
方法不再存在。
public function download()
{
$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
, 'Content-type' => 'text/csv'
, 'Content-Disposition' => 'attachment; filename=galleries.csv'
, 'Expires' => '0'
, 'Pragma' => 'public'
];
$list = $this->users->getAllUsers()->toArray();
# add headers for each column in the CSV download
array_unshift($list, array_keys($list[0]));
$callback = function() use ($list)
{
$FH = fopen('php://output', 'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
};
// return Response::stream($callback, 200, $headers); // Old version
return response()->download($callback, 'Users-' . date('d-m-Y'), $headers);
}
我试图使用Laravel 5.2 response()
功能,但我刚开始有点失落,什么我与响应 - download()
看来合理的选择,但是这给了我下面的错误:
Object of class Closure could not be converted to string
这是有道理的。什么是正确的方式去做这件事?或者我应该保存文件,然后使用文件路径作为我的download()
函数的第一个参数 - 这似乎是不好的做法?