2017-10-24 4 views
0

Excel CSVシートを作成してダウンロードしようとしています。Laravel Returnダウンロードが動作しませんでした

CSVファイルは完全に作成されていますが、残念ながらダウンロードできませんでした。

public function download_csv(Request $request) { 
     if ($request->isMethod('post')) { 
      if (Auth::check()) { 

      $user_id = Auth::id(); 
      $customer_id = Auth::user()->customer_id; 

      $suppliers = Supplier::where('is_active', 1)->get(); 

      $dataCSV = array(); 
      $main = array(); 
      $cnt = 1; 
      foreach($suppliers as $m) { 
       $main['S. No'] = $cnt++; 
       $main['Name'] = $m->Name; 
       array_push($dataCSV, $main); 
       $cnt++; 
      } 

      $csv_name = $user_id.'_'.rand(10000, 99999); 
      $csv = Excel::create($csv_name, function($excel) use($dataCSV) { 
       $excel->sheet('Sheetname', function($sheet) use($dataCSV) { 
        $sheet->fromArray($dataCSV); 
       }); 
      }); 
      $csv->store('csv', storage_path('excel/exports/'.$customer_id.'/')); 
      $file = storage_path('excel/exports/'.$customer_id.'/'.$csv_name.'.csv'); 

      $headers = array(
       'Content-Type: application/csv', 
      ); 

      return Response::download($file, $user_id.'.csv', $headers); 

      } 
     } 
    } 

私はどこに問題があるのか​​分かりませんでした。

ご協力いただきますようお願い申し上げます。

ありがとうございました

+0

、あなたはページを更新する必要があります。 –

答えて

0

レスポンスクラスを使用しましたか? use Illuminate\Support\Facades\Response;

オプション2:別の方法を経由して、それをダウンロードしてみてください:ダウンロードのために return response()->download($pathToFile, $name, $headers);

https://laravel.com/docs/5.4/responses#file-downloads

関連する問題