将大型Excel工作表记录上传到我的数据库时,我收到MethodNotAllowedHttpException

时间:2016-06-17 22:06:20

标签: php laravel maatwebsite-excel

我正在尝试将数据从excel文件导入我的数据库,问题是当我上传太多记录时,我收到此错误消息

  

RouteCollection.php第219行中的MethodNotAllowedHttpException

这是我的代码:

route.php

Route::group(['prefix' => 'panel', 'middleware' => 'adminMiddleware'], function () {
    Route::get('/index', 'admin\AdminController@index');
    Route::post('/uploadUsersExcel', 'admin\AdminController@uploadUsersExcel');
    Route::post('/downloadUsersExcel', 'admin\AdminController@downloadUsersExcel');

});

AdminController.php

  public function uploadUsersExcel(Request $request)
    {


        if ($request->file('file')) {
            $name = 'uploads/csv/' . $request->file('file')->getClientOriginalName();
            $request->file('file')->move('uploads/csv/', $name);
            DB::table('users')->whereNotIn('id', [1])->delete();

            Excel::filter('chunk')->load($name)->chunk(250, function($reader)

            {

                $data = $reader->toArray();

                foreach ($data as $value) {
                    $user = new user();
                    $user->password = bcrypt($value['password']);
                    $user->save();
               }
            }

            );

        }

    }

users.blade.php

            {!! Form::open(['files'=>true,'method'=>'post','url'=>'panel/uploadUsersExcel']) !!}
                        <input class="form-control" type="file" name="file"/>
                        <button type="submit"  class="bigger-icon btn btn-block  btn-success">


upload
                        </button>
                        {!! Form::close() !!}

1 个答案:

答案 0 :(得分:0)

首先,

enctype添加到表单标记。

 {!! Form::open(['files'=>true,'method'=>'post','url'=>'panel/uploadUsersExcel', 'enctype' => 'multipart/form-data']) !!}

如果您上传的文件大于php.ini php.ini中增加 post_max_size upload_max_filesize >