[Vuejs]-How to import excel file in mysql using laravel 5.7 with vue JS

0👍

this is vue views and js my code :

<form method="POST" action="#" class="form-horizontal" @submit.prevent="createUpload" enctype="multipart/form-data">

              <div class="box-body">
                <div class="form-group row"><label for="name" class="col-sm-2 form-control-label">Parameter</label>
                  <div class="col-sm-10">
                  <input type="file" class="form-control" id="result_file" ref="myFiles" placeholder="Name Parameter" required="required" @change="previewFiles($event)">

                  <input type="hidden" name="result_file2" class="form-control" id="title" v-model="datafileupload.result_file3"  placeholder="Name Parameter" required="required">
                  <input type="hidden" name="hasilcek" class="form-control" id="hasilcek" v-model="datafileupload.hasilcek3"  placeholder="Name Parameter" required="required">

                  <input type="hidden" class="form-control" v-model="datapush.filename">
                  <input type="hidden" class="form-control" v-model="datapush.size">
                  </div><br><br><br>
                  <div v-if="this.hasilcekexcel > 0" class="alert alert-warning">File {{ namafile }}  exists , do you want to replace it ?</div>
              </div>
            </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" @click="doSomethingOnHidden" >Close</button>
                <input type="submit" class="btn btn-primary"  id="submit_upload2" v-if="this.hasilcekexcel > 0" value="Replace">
                <input type="submit" class="btn btn-primary"  id="submit_upload" v-if="this.hasilcekexcel == 0" value="Upload">

              </div>
            </form>

js upload :

 createUpload () {
   var data = new FormData();
   var file = this.$refs.myFiles.files[0];
   var size = this.$refs.myFiles.files[0].size;

   data.append('filenya', file);
   data.append('namasifile', this.datafileupload.result_file3);
   data.append('angkacek', this.datafileupload.hasilcek3);


   axios.post('excelupload/upload', data)
     .then(response => {
     this.pesertas.push(response.data.data);
         this.showModal =false;

     });
  },

controller :

 $excelupload = new Exceluploads;
      $hasilcek = $request->angkacek;
      $requestfile = $request->namasifile;
      if ($request->filenya != '') {
          $input = Input::all();
          $size = $request->file('filenya')->getClientSize();

          $file = array_get($input,'result_file');
          $userID = Auth::user()->id;
          $uploaded_by = Auth::user()->name;

          $destinationPath  = 'excel';

            // GET THE FILE EXTENSION

                $extension = 'xls';

            $fileName = $requestfile.'.'.$extension;
          if ($hasilcek > 0) {
              File::delete($destinationPath.'/'.$fileName);
              $upload_success = $request->filenya->move($destinationPath, $fileName);
                return response()->json(['data' => Exceluploads::find($excelupload->id)]);
          }else if ($hasilcek == 0) {
              $upload_success = $request->filenya->move($destinationPath, $fileName);
              $excelupload->filename = $fileName;
              $excelupload->directory = $destinationPath.'/'.$fileName;
              $excelupload->size = $size;
              //1 = not custome
              $excelupload->type = 1;
              $excelupload->uploaded_by = $uploaded_by;
              $excelupload->save();
              return response()->json(['data' => Exceluploads::find($excelupload->id)]);
          }
      }

Leave a comment