BigJobbies
BigJobbies

Reputation: 509

Laravel 4 - Downloading a CSV and returning a true response

I was wondering if someone could help me out.

I have some code which takes an array and puts it into csv format, at the moment, it saves the file to the server, but i want the user to be able to download the csv instead and nothing im trying is working.

Currently my function looks like this

public function postForm() {

    $exportType = Input::get('exportType');
    $data = Session::get('data');

    if ( $exportType == 'csv' ) {

        $fp = fopen('csvExport.csv', 'w');

        foreach( $data as $fields ) {
            fputcsv($fp, $fields);
        }

        fclose($fp);

    } else {

        // Other function here

    }

    return Response::json( array('result' => true) );

}

Can anyone help me out please, any help would be really greatly appreciated.

Cheers,

Upvotes: 0

Views: 270

Answers (1)

Laurence
Laurence

Reputation: 60088

If you want the user to download the file - you need to 'return' the file to them as the Response

 return Response::download('csvExport.csv');

Upvotes: 1

Related Questions