我无法上传文件

时间:2018-05-07 11:13:57

标签: laravel laravel-excel

我想导入和导出excel文件,这些是我的代码:

这是CustomerController;

 public function customersImport(Request $request)
{
    if($request->hasFile('customers')) {
        $path = $request->file('customers')->getRealPath();
        $data = \Excel::load($path)->get();
        if ($data->count()) {
            dd($value);
            foreach ($data as $key => $value) {
                $customer_list[] = ['name' => $value->name, 'surname' => $value->surname, 'email' => $value->email];
            }
            if (!empty($customer_list)) {
                Customer::insert($customer_list);
                \Session::flash('Success', 'File imported succesfully');
            }
        }
    }
        else{
            \Session::flash('warning','There is no file to import');
        }
        return Redirect::back();
    }

这是我的customers.blade;

@extends('layouts.app')

@section('content')
<div class="panel-heading">Import and Export Data Into Excel File</div>

<div class="panel-body">
    {!!Form::open(array('route'=>'customer.import','method'=>'POST','files'=>'true')) !!}
    <div class="row">
        <div class="col-xs-10 col-sm-10 col-md-10">
            @if(Session::has('success'))
                <div class="alert alert-success">
                    {{Session :: get('message')}}
                </div>
            @endif
            @if(Session::has('warning'))
                <div class="alert alert-warning">
                    {{Session::get('message')}}
                </div>
            @endif
            <div class="form-group">
                {!! Form::label('sample_file','Select File to Import:', 
             ['class'=>'col-md-3']) !!}
                <div class="col-md-9">
                    {!! Form::file('customers',array('class'=>'form-control')) !!}
                    {!! $errors->first('products','<p class="alert alert-danger">:message</p') !!}
                </div>
            </div>
        </div>
        <div class="col-xs-2 col-sm-2 col-md-2 text-center">
            {!! Form::submit('Upload',['class'=>'btn btn-success']) !!}
        </div>
    </div>
    {!! Form::close() !!}
 </div>
 @endsection

当我点击上传文件错误时说:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 没有消息

你能帮我解决一下吗?我不知道什么是错的,屏幕上的错误信息不清楚:(

1 个答案:

答案 0 :(得分:1)

你正在发布GET路线。 (查看here以获取有关不同HTTP谓词的更多信息)

将路线更改为Route::post('customer-import', 'CustomerController@customersImport')->name('customer.import');应修复此错误。