Laravel MethodNotAllowedHttpException(405)文件上传

时间:2019-02-20 18:16:22

标签: php laravel

我正在尝试上载csv文件,然后转储标头信息以测试文件上载是否可行。我可以看到页面并选择要上传的文件。但是,当我尝试提交时,遇到了以下错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

开发人员工具输出:upload-report:1 POST (url)/upload-report 405 (Method Not Allowed)

尝试研究该问题表明它可能是路线问题,但我似乎找不到一个问题。

路线

Route:: get('upload-prescriptions', 'PrescriptionController@uploadPage')->name('upload-prescriptions');
Route:: get('upload-report', 'PrescriptionController@uploadReport')->name('upload-report');

控制器

public function uploadPage()
{
    return view('prescriptions.upload');
}


public function uploadReport(Request $request)
{
    $upload = $request->file('upload_file');

    $file_path = $upload->getRealPath();

    $file = fopen($file_path, 'r');

    $header = fgetcsv($file);

    dd($header);
}

刀片

{!! Form::open(['action' => ['PrescriptionController@uploadReport'], 'method' => 'POST', 'enctype' => 'multipart/form-data', 'files' => true]) !!}
    @csrf
    {{Form::file('upload_file', ['class' => 'form-control-file'])}}
    {{Form::submit('Upload File', ['class' => 'btn btn-success'])}}
{!! Form::close() !!}

2 个答案:

答案 0 :(得分:1)

Route:: get('upload-report', 'PrescriptionController@uploadReport')->name('upload-report');

更改为

Route::post('upload-report', 'PrescriptionController@uploadReport')->name('upload-report');

因为您在表单中将方法表示为POST,但您的路线是get

答案 1 :(得分:0)

在您的查看文件中,您已使用过后提交请求表单

Form::open(['action' => ['PrescriptionController@uploadReport'], 'method' => 'POST', 'enctype' => 'multipart/form-data', 'files' => true])

但是您在route中将方法定义为get方法

Route:: get('upload-report', 'PrescriptionController@uploadReport')->name('upload-report');

所以请按如下所示更改为发帖请求

Route::post('upload-report', 'PrescriptionController@uploadReport')->name('upload-report');