0

Im trying to figure out a way i can return a view but also call the download function. Something like this:

return view('home')->download($filename, 'text.csv', $headers);

2 Answers2

2

I don't think that it's possible. Laravel has to choose what to send to your browser. If it sends a redirect header you won't see the page.

The only solution is to render the view and in the view put a simple javascript like:

window.location.href = "<your file url>"

If this is a file it should start a download without changing page.

wezzy
  • 5,897
  • 3
  • 31
  • 42
0

Currently there's a way to do it using a stream download. It's not pretty, but it works. Note the function has to echo and not return, hence why render() is called on the view.

return response()->streamDownload(function() {
        echo view('home')->render();
    }
    , 'test.csv'
    , ['Content-Type' => 'text/csv']
);
dsturbid
  • 1,899
  • 1
  • 17
  • 24