如何在laravel中上传CSV文件

时间:2017-11-14 13:16:17

标签: laravel csv laravel-5

我正在尝试在我的laravel项目中上传CSV文件,但每次都要投掷

Whoops! There were some problems with your input.

The file failed to upload.

谁能告诉我这是什么问题。 这是我的代码

路线档案:

Route::Get('upload','AdminVideoController@upload');
Route::Post('import-data','AdminVideoController@importData')->name('import-data');

控制器:

 //File upload form
    public function upload(){
        return view('pages.admin.fileupload');
    }

    //import data
    public function importData(Request $request)
        {
            $validator = Validator::make($request->all(), [
                'file' => 'required'
            ]);
            if ($validator->fails()) {
                return redirect()
                    ->back()
                    ->withErrors($validator);
            }
            $file = $request->file('file');
            $csvData = file_get_contents($file);
            dd($csvData);
        }

Fileupload Blade

 <form class="fileupload" action="{{route('import-data')}}" method="POST" enctype="multipart/form-data">
  {{csrf_field()}}
  <div class="row">
    <div class="col-lg-6 col-md-6 col-xs-12">
        <div class="form-group">
            <label for="file">Select a file to upload</label>
               <input type="file" name="file" id="file" class="form-group">
         </div>
      </div>
      <div class="col-xs-12">
          <button type="submit" class="btn btn-primary">Upload</button>
     </div>
   </div>
 </form>

这似乎是一个重复的帖子,但我尝试了这些解决方案,但没有成功。 任何人都可以看看我的代码,让我知道问题在哪里?

0 个答案:

没有答案