mardi 21 avril 2020

upload file PDF in Laravel Using Vue -Error 422

I want to upload pdf in my project using laravel and vuejs but it shows 422 (Unprocessable Entity).any help guys? the file can't pass to the controller the input is empty when i submit? this vue:

<form  @submit="ajouterReclamtion" enctype="multipart/form-data">
  <div class="form-group"  >
    <div class="input-group mt-2">
    <input type="file" :class="{ 'is-invalid': form.errors.has('file') }" name="file" 
    v-on:change="onFileChange">
    <has-error :form="form" field="file"></has-error>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
     <button  type="submit" class="btn btn-primary" >Add</button>
  </div>
</form>

and this the script part:

 onFileChange(e){
  console.log(e.target.files[0]);
  this.file = e.target.files[0];
  },
  ajouterReclamtion(){
  e.preventDefault();
  let currentObj = this;
  const config = {
  headers: { 'content-type': 'multipart/form-data' }
  }
  let formData = new FormData();
  formData.append('file', this.file);
 this.form.post('api/reclamation', formData, config).then(()=>{
 fire.$emit('ajoutreclamation');
 $("#ajouterReclamation").modal('hide');
 Toast.fire({
 icon: 'success',
 title: 'Réclamation created'
  })
 currentObj.success = response.data.success;
}).catch(function (error) {
currentObj.output = error;
});
},

and this is the controller:

  public function store(Request $request)
 {
$this->validate($request,[

'file' => 'required|mimes:doc,docx,pdf,txt|max:2048'
  ]);
 $data = $request->all();
 $extention = time().'.'.$request->file->getClientOriginalExtension();
  $fileName =$request->name.'.'.$extention;
  $request->file->move(public_path('upload'), $fileName);
  $reclamation->file= $fileName;
  $reclamation->save();
    } 


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire