2016-06-11 15 views
0

laravel用のmaatwebsite/excelパッケージをインストールしました。それはうまく動作します。私の問題は、ファイルをチェックした後にそれを処理する方法です。Laravel Excelの検証

アップロード()私はフォーラムをファイルに表示される機能と、私はファイルをアップロードするには、ここにある:

public function upload(Request $request) { 

    $orientation = $request->get('orientation'); 
    if($request->file('file')) { 
     $status = Excel::load(Input::file('file'), function ($reader) use ($orientation) { 
      return self::upload_excel_files($reader, $orientation); 
     }); 
     if($status) { 
      Session::flash('status', "Excel data was imported successfully"); 
      return redirect(route('admin.exchange.upload')); 
     } 
     else { 
      return redirect(route('admin.exchange.upload'))->withErrors('Invalid format. Please check the XLS template.'); 
     } 
    } 
    return view('admin.exchange.upload'); 

} 

public function upload_excel_files($file, $orientation) { 
    if(something) return true; 
    else return false; 
} 

upload_excel_files機能をチェックファイルには、適切なフォーマットを持っているし、そうならば、それはそれを処理する場合と、成功したメモを返さなければなりません。ファイルが要求フォーマットと一致しない場合は、エラーメッセージを出す必要があります。

残念ながら、私はユーザーメッセージをどこに置くべきかわかりません。この例のように、常に成功したメッセージを返します。何らかの理由でメッセージがupload_excel_files()関数内にある場合、最初のリダイレクトでメッセージが取得されないので、2番目のリダイレクトで奇妙なメッセージが表示されます。

どうすればいいですか?

チェックの

答えて

0

ファイル適切なフォーマットを使用laravel要求

public function rules() 
    { 
     return [ 
      'file' => 'required|mimeTypes:'. 
       'application/vnd.ms-office,'. 
       'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,'. 
       'application/vnd.ms-excel', 
     ]; 
    } 

詳細MIMEタイプがある場合:エラーメッセージの https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

はこれを読んで:

https://laravel.com/docs/5.4/validation#working-with-error-messages