我无法上传文件
I can not upload file
我想导入和导出 excel 文件,这些是我的代码:
这是客户控制器;
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
没有消息
你能帮我解决一下吗?我不知道出了什么问题,屏幕上的错误信息也不清楚:(
您正在 POST 到 GET 路由。 (查看 here 了解更多关于不同 HTTP 动词的信息)
将您的路线更改为 Route::post('customer-import', 'CustomerController@customersImport')->name('customer.import');
应该可以修复此错误。
我想导入和导出 excel 文件,这些是我的代码:
这是客户控制器;
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 没有消息
你能帮我解决一下吗?我不知道出了什么问题,屏幕上的错误信息也不清楚:(
您正在 POST 到 GET 路由。 (查看 here 了解更多关于不同 HTTP 动词的信息)
将您的路线更改为 Route::post('customer-import', 'CustomerController@customersImport')->name('customer.import');
应该可以修复此错误。