0

我在尝试使用 axios 将 excel 文件导出到数据库时遇到问题。我能够控制文件,但无法弄清楚发布数据。我收到此错误无法加载资源:服务器响应状态为 422(无法处理的实体)app.js:716

我的观点

 <form  enctype="multipart/form-data">
   @csrf
    <div class="form-group">
     <table class="table">
      <tr>
       <td width="40%" align="right"><label>Select File for Upload</label></td>
       <td width="30">
        <input type="file"   @change="getExcelData" name="select_file"  />
       </td>
       <td width="30%" align="left">
        <input type="submit" name="upload" class="btn btn-primary" value="Upload">
       </td>
      </tr>
     </table>
    </div>
   </form>

vue 函数

getExcelData(e){
       //const formData = new FormData();
    const file = e.target.files[0];
     console.log(file);
    //formData.append('file', file);
 axios.post("/import_excel/import").then(function(response){
   // this.users=response.data;
   console.log("working now");
   console.log(file);
    console.log(response)
          }.bind(this))
    },

我的控制器文件

function import(Request $request)
    {
     $this->validate($request, [
      'select_file'  => 'required|mimes:xls,xlsx'
     ]);

     $path = $request->file('select_file')->getRealPath();

     $data = Excel::load($path)->get();

     if($data->count() > 0)
     {
      foreach($data->toArray() as $key => $value)
      {
       foreach($value as $row)
       {

        $insert_data[] = array(
         'userName'  => $row['userName'],
         'userPassword'   => $row['userPassword'],
         'timePackage'   => $row['timePackage'],
         'location'    => $row['location'],
         'Assigned'    => $row['Assigned']

        );
       }
      }

      if(!empty($insert_data))
      {
       DB::table('hotspot_users')->insert($insert_data);
      }
     }
     return back()->with('success', 'Excel Data Imported successfully.');
    }

路线

Route::post('/import_excel/import', 'ImportExcelController@import');
4

0 回答 0