2015-12-09 51 views
2

Laravel Excel文檔中找不到如何在download()之後重定向()。我試圖做這樣的行爲如何在使用Laravel Excel後 - >下載()後重定向

Excel::load('/bills/bill.template.xlsx', function($doc) { 
    $sheet = $doc->setActiveSheetIndex(0); 
    $sheet->setCellValue('G21', '{buyer}'); 
})->download('xlsx'); 

return redirect('/')->with('notification','custom msg'); 

但它不起作用。

回答

0

要下載,您必須執行return語句以及下載。

return Excel::load('/bills/bill.template.xlsx', function($doc) { 
    $sheet = $doc->setActiveSheetIndex(0); 
    $sheet->setCellValue('G21', '{buyer}'); 
})->download('xlsx'); 

如果您不將它發送到視圖,它不會自行下載。 因此,據我所知,沒有像下載和重定向這樣的事情。

您可以做的是將Excel文件存儲在laravel的存儲文件夾(或任何你喜歡的地方)的某處,然後重定向。在新頁面中,您可以使用JS打開一個隱藏頁面,並在該頁面中下載您想要提供給用戶的Excel文件。

+0

它的工作原理應該沒有返回語句 –

0

似乎不能這樣做,但有一個變通方法,你可以嘗試在一個類似的問題:

How do I redirect after download in Laravel?

這基本上你重定向到最後一頁第一然後開始下載:

Session::flash('download.in.the.next.request', 'filetodownload.pdf'); 

return Redirect::to('/'); 

然後在你的看法會是這個樣子:

<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> 

查看以上鍊接以獲得進一步解釋。