微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用vue Js和Laravel将数据从excel文件导入mysql

如何解决如何使用vue Js和Laravel将数据从excel文件导入mysql

我在尝试使用 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');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。