midwestdev
9 months ago
266
2
Laravel

StreamDownload not forcing download in browser

Posted 9 months ago by midwestdev

Working on a simple script to take some data from an odbc connection and then converting it to a csv. I wrote the following


    public function Export($data) {
        $headers = array(
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=summary.csv",
            "Pragma" => "no-cache",
            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
            "Expires" => "0"
        );
        $columns = array('Period', 'Revenue Area', 'Subs');
       
        $callback = function() use ($data, $columns)
        {
            $file = fopen('php://output', 'w');
            fputcsv($file, $columns);
            
            foreach($data as $items) {
                fputcsv($file, array($items->PERIOD, $items->REVENUE_AREA, $items->SUBS));
                
            }
            fclose($file);
        };
        return response()->streamDownload($callback, 'prefix-' . date('d-m-Y-H:i:s').'.csv', $headers);
        
    }

The data is there, I can dump it. There are no errors , but the browser is just not forcing the download. Any help or suggestions would be greatly appreciated.

Please sign in or create an account to participate in this conversation.