导出 CSV 响应 laravel 5.5 并下载为 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48802700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:23:52  来源:igfitidea点击:

Exporting CSV response laravel 5.5 and download as csv file

laravelexport-to-csv

提问by Nurul Alam

I am trying to export and download some data in a csv file using an ajax request. I was able to output the data in a json response to test but could not get it to download in a data.csv file

我正在尝试使用 ajax 请求导出和下载 csv 文件中的一些数据。我能够在 json 响应中输出数据以进行测试,但无法将其下载到 data.csv 文件中

Following is the code I have written so far

以下是我到目前为止编写的代码

   public function download(Request $request)
    {

        if(!empty($request->input('my_checkbox')))
        {
             $studentIdToExportArray = $request->input('my_checkbox');
            //$msg = is_array($studentIdToExportArray);//returns true
            $selectedStudents = [];   
            foreach($studentIdToExportArray as $student_id)
            {
                    $student = Students::find($student_id);
                    array_push($selectedStudents, $student);
            }
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=data.csv');
            $output=fopen("php://output","w");
            fputcsv($output,array("ID","firstname","surname","email","nationality","address_id","course_id"));

            foreach($selectedStudents as $s1)
            {
                $array = json_decode(json_encode($s1), true);
                fputcsv($output, $array);
            }
            fclose($output);    

            return response()->json([
                   'test'=> $selectedStudents 
            ]);           
        }


    }

Screenshot of output response in console

控制台中输出响应的屏幕截图

enter image description here

在此处输入图片说明

Problem: The file data.csv does not download

问题:文件data.csv不下载

采纳答案by Sapnesh Naik

Try:

尝试:

$file=fopen('../storage/app/test.csv','w');

$headers = array(
    'Content-Type' => 'text/csv',
);

$path = storage_path('test.csv');
return response()->download($path, 'test.csv', $headers);

回答by Christophvh

You first have to save the file to a location on your local or a cloud disk and then do a file response or a download response. These are the download options like shown in the docs: https://laravel.com/docs/5.5/responses#file-responses

您首先必须将文件保存到本地或云盘上的某个位置,然后进行文件响应或下载响应。这些是文档中显示的下载选项:https: //laravel.com/docs/5.5/responses#file-responses

return response()->download($pathToFile);

return response()->download($pathToFile, $name, $headers);

return response()->download($pathToFile)->deleteFileAfterSend(true)

ps: you can also look at using a dedicated package for this, or just use the package as a reference. https://github.com/Maatwebsite/Laravel-Excel

ps:您也可以考虑为此使用专用包,或者仅使用该包作为参考。https://github.com/Maatwebsite/Laravel-Excel