Getting "Undefined offset: 1" while upload file on vuejs – php [duplicate]

  file-upload, laravel, php, vue.js

Getting error while updating existing image file on the edit page. on create page, it’s working fine. how can I get upload file info without clicking on onchange function?

on create page
on edit page

On Php file

if($request->get('img')) //image upload
        {
            $image = $request->get('img');
            $name = time().'.' . explode('/', explode(':', substr($image, 0, strpos($image, ';')))[1])[1];
            Image::make($request->get('img'))->save(public_path('img/').$name);
        }else{
            $name='';
        }

vueJs

<input class="img-value"
       ref="reset_bg_img"
       @change="img_upload($event)"
       value=""
       accept="image/*"                  
       id="BG-img" >

export default {
  name: "dummy",
  
   data() {
    return {
        bg_img:'' 
     }
   }
}

created() {
            axios.get("/view_schedule_quiz/" + this.$route.params.id)
            .then(response => {
                this.bg_img = '/quiz_img/'+response.data.bg_image;
            }.catch(error => console.log(error));
},

schedule_img_upload(e){
      let files = e.target.files || e.dataTransfer.files;

      const file = e.target.files[0];
      this.bg_img = URL.createObjectURL(file);

      if (!files.length)
        return;
      this.createImage(files[0]);
    },
    createImage(file) {
      let reader = new FileReader();
      let vm = this;
      reader.onload = (e) => {
        vm.bg_img = e.target.result;
      };
      reader.readAsDataURL(file);
    },

Source: Laravel

Leave a Reply