Jofran
Jofran

Reputation: 79

Laravel 5 Generated .txt not redirecting to view

I have a Laravel web app that generates a formatted .txt and then has the browser download it, and refreshing the form after everything is done, the .txt is generated correctly but I'm not getting a redirect, how can I make a redirect and also have the file downloaded? I wouldn't mind if it needs to be changed to a save window and have the user select the location, but otherwise it can't save directly to the server.

Here is the code that generates the .txt in my controller.

$filename = "Payments-".date('d-M-Y').".txt";
        $f = fopen($filename, 'w');
        fwrite($f, $header.PHP_EOL.$payments);
        fclose($f);

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Length: ". filesize("$filename").";");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/octet-stream; ");
        header("Content-Transfer-Encoding: binary");

        readfile($filename);

        //exit;

        return view('index')->with('date', $date->format('d-M-y'));

Upvotes: 0

Views: 118

Answers (1)

user4488093
user4488093

Reputation:

For redirect:

Use Laravel redirect helper:

return redirect('some-url')->with('date', $date->format('d-M-y'));

For download file use enter link description here this:

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

Upvotes: 1

Related Questions