How do I redirect after download in Laravel?
that worked
I gave up from using ajax and just tried with routes
Route::get('relatorios/exportar/{cod}', array('as' => 'relatorios.exportar', 'uses' => 'ReportsController@exportar'));
my controller
public function exportar($cod)
{
set_time_limit(0);
$datainicio = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_inicio');
$datafinal = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_fim');
$mes = DB::table('tb_periodo')->where('cod', $cod)->pluck('mes_referencia');
$horarioQuery = $this->horario->with(array('funcionario', 'item_contabil'))
->whereBetween('data', array($datainicio, $datafinal))
->whereNull('deleted_at')
->orderBy('cod_funcionario')
->orderBy('data', 'ASC')
->get();
$horarios = reset($horarioQuery);
$nome = 'Marcações'.$mes.'-'.Carbon::now()->year;
$this->horario->allToExcel($nome, $horarios);
}
view:
{{ link_to_route('relatorios.exportar', 'Exportar para excel', array($cod), array('class' => 'btn btn-success')) }}
that's solved for me, because dont load another page and download the correct file. thx for the help!!
It cannot be done. The problem is that if you send a download instruction to the user browser, you are, as a matter of fact, sending a response and you can send only one response back.
What you could do is to, first redirect your user to the "final" page and in that page start the download. The code would be something like:
Session::flash('download.in.the.next.request', 'filetodownload.pdf');
return Redirect::to('/whatever/page');
Then in your new page you'll have some options:
- HTML: [meta http-equiv="refresh" content="5;url=http://watever.com/create_csv.php"]
- Javascript: location.href = 'http://watever.com/create_csv.php';
- iframe: [iframe src="create_csv.php"][/iframe]
So you can in your layout do something like:
<html>
<head>
@if(Session::has('download.in.the.next.request'))
<meta http-equiv="refresh" content="5;url={{ Session::get('download.in.the.next.request') }}">
@endif
<head>
<body>
...
</body>
</html>
Also, take a look at this answer: PHP generate file for download then redirect