【问题标题】:How to redirect after ->download() with Laravel Excel如何使用 Laravel Excel 在 ->download() 之后重定向
【发布时间】:2016-03-15 04:02:48
【问题描述】:

在 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');

但它不起作用。

【问题讨论】:

    标签: php excel laravel


    【解决方案1】:

    似乎无法完成,但您可以尝试解决类似的问题:

    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>
    

    查看上面的链接以获得进一步的解释。

    【讨论】:

      【解决方案2】:

      要下载,您必须在下载的同时执行 return 语句。

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

      如果您不将其发送到视图,它将不会自行下载。 所以,据我所知,没有下载和重定向这样的东西。

      您可以做的是将Excel文件存储在laravel的存储文件夹中的某个位置(或任何您喜欢的位置),然后重定向。在新页面中,您可以使用 JS 打开一个隐藏页面,然后在该页面中下载您要提供给用户的 excel 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        • 2018-01-29
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多