控制者

public function uploadFile()
{
      if(isset($_POST["uploadFile"])){
        $filePath = realpath($_FILES["file"]["tmp_name"]); //GETTING THE FULL PATH OF THE FILE WHICH WANTS TO BE SAVED INTO THE DB
        $arr = [];
            if (($handle = fopen($filePath, 'r')) !== FALSE){
                $i = 0;
                while (($lineArray = fgetcsv($handle, 4000)) !== FALSE){
                    for ($j=0; $j<count($lineArray); $j++){
                        $arr[$i][$j] = $lineArray[$j];
                    } $i++;
                } fclose($handle);
            }
        $csv = array_slice($arr,1);
        foreach($csv as $line){
          mysqli_query($mysqli, "INSERT INTO dialog(`eType`,`eVal`,`intent`,`reply`) VALUES('$line[0]','$line[1]','$line[2]','$line[3]')"); //FOR EACH LINE SAVE INTO THE DB
        }
      }
}


路线

Route::get('uploadFile', 'Chatbot\ChatbotController@uploadFile');


视图

<form method="post" action="{{action('AltHr\Chatbot\ChatbotController@uploadFile')}}" enctype="multipart/form-data">
      <input type="file" name="file"> <br>
      <button type="submit" name="uploadFile" class="btn alt-btn-black btn-xs alt-btn">UPLOAD FILE</button>
</form>


所以,这是我学习laravel的第一天,有人可以告诉我为什么我在RouteCollection.php中有这个错误MethodNotAllowedHttpException

我正在尝试将表单中的CSV文件上传到数据库。我在这里做错了什么?

最佳答案

您的路线错误。上载是POST请求,因此:

Route::post('uploadFile', 'Chatbot\ChatbotController@uploadFile');

10-08 09:32