Laravel + Vue $request->hasFile('image') returns null











up vote
1
down vote

favorite













  • Laravel 5.7

  • upload_max_filesize = 200m

  • post_max_size = 250m


$request->hasFile('image') is returning null, even though I can clearly see the request holding an image key returning with a valid base64 (validated it to be an image)



controller



if ($request->hasFile('image')) {
return 'yes';
}else{
echo 'no';
return $request->all();
}


front



<form @submit.prevent="saveOoi" enctype="multipart/form-data">
<div><img :src="image" class="embed-responsive"></div>
<input type="file" v-on:change="onFileChange" class="form-control mb-3" name="image">
</form>


scripts



data() {
return {
image: '',
errors: new Errors(),
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
this.$data.image = files[0];
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
saveOoi() {
axios.post('ooi', this.$data)
.then(response => console.log('sent', response))
.catch(error => this.errors.record(error.response.data));
}
}


i have the vue extension for chrome and I can see that the file is set in data.image when I pick the file.



What am I missing here?










share|improve this question






















  • if you show me the result of your $request->all() when sending images, that would be helpful.
    – AmirhosseinDZ
    Nov 12 at 10:05










  • @AmirhosseinDZ sure => prntscr.com/lhaod7
    – clusterBuddy
    Nov 12 at 10:07















up vote
1
down vote

favorite













  • Laravel 5.7

  • upload_max_filesize = 200m

  • post_max_size = 250m


$request->hasFile('image') is returning null, even though I can clearly see the request holding an image key returning with a valid base64 (validated it to be an image)



controller



if ($request->hasFile('image')) {
return 'yes';
}else{
echo 'no';
return $request->all();
}


front



<form @submit.prevent="saveOoi" enctype="multipart/form-data">
<div><img :src="image" class="embed-responsive"></div>
<input type="file" v-on:change="onFileChange" class="form-control mb-3" name="image">
</form>


scripts



data() {
return {
image: '',
errors: new Errors(),
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
this.$data.image = files[0];
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
saveOoi() {
axios.post('ooi', this.$data)
.then(response => console.log('sent', response))
.catch(error => this.errors.record(error.response.data));
}
}


i have the vue extension for chrome and I can see that the file is set in data.image when I pick the file.



What am I missing here?










share|improve this question






















  • if you show me the result of your $request->all() when sending images, that would be helpful.
    – AmirhosseinDZ
    Nov 12 at 10:05










  • @AmirhosseinDZ sure => prntscr.com/lhaod7
    – clusterBuddy
    Nov 12 at 10:07













up vote
1
down vote

favorite









up vote
1
down vote

favorite












  • Laravel 5.7

  • upload_max_filesize = 200m

  • post_max_size = 250m


$request->hasFile('image') is returning null, even though I can clearly see the request holding an image key returning with a valid base64 (validated it to be an image)



controller



if ($request->hasFile('image')) {
return 'yes';
}else{
echo 'no';
return $request->all();
}


front



<form @submit.prevent="saveOoi" enctype="multipart/form-data">
<div><img :src="image" class="embed-responsive"></div>
<input type="file" v-on:change="onFileChange" class="form-control mb-3" name="image">
</form>


scripts



data() {
return {
image: '',
errors: new Errors(),
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
this.$data.image = files[0];
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
saveOoi() {
axios.post('ooi', this.$data)
.then(response => console.log('sent', response))
.catch(error => this.errors.record(error.response.data));
}
}


i have the vue extension for chrome and I can see that the file is set in data.image when I pick the file.



What am I missing here?










share|improve this question














  • Laravel 5.7

  • upload_max_filesize = 200m

  • post_max_size = 250m


$request->hasFile('image') is returning null, even though I can clearly see the request holding an image key returning with a valid base64 (validated it to be an image)



controller



if ($request->hasFile('image')) {
return 'yes';
}else{
echo 'no';
return $request->all();
}


front



<form @submit.prevent="saveOoi" enctype="multipart/form-data">
<div><img :src="image" class="embed-responsive"></div>
<input type="file" v-on:change="onFileChange" class="form-control mb-3" name="image">
</form>


scripts



data() {
return {
image: '',
errors: new Errors(),
}
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length)
return;
this.createImage(files[0]);
this.$data.image = files[0];
},
createImage(file) {
let reader = new FileReader();
let vm = this;
reader.onload = (e) => {
vm.image = e.target.result;
};
reader.readAsDataURL(file);
},
saveOoi() {
axios.post('ooi', this.$data)
.then(response => console.log('sent', response))
.catch(error => this.errors.record(error.response.data));
}
}


i have the vue extension for chrome and I can see that the file is set in data.image when I pick the file.



What am I missing here?







laravel vue.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 9:24









clusterBuddy

514314




514314












  • if you show me the result of your $request->all() when sending images, that would be helpful.
    – AmirhosseinDZ
    Nov 12 at 10:05










  • @AmirhosseinDZ sure => prntscr.com/lhaod7
    – clusterBuddy
    Nov 12 at 10:07


















  • if you show me the result of your $request->all() when sending images, that would be helpful.
    – AmirhosseinDZ
    Nov 12 at 10:05










  • @AmirhosseinDZ sure => prntscr.com/lhaod7
    – clusterBuddy
    Nov 12 at 10:07
















if you show me the result of your $request->all() when sending images, that would be helpful.
– AmirhosseinDZ
Nov 12 at 10:05




if you show me the result of your $request->all() when sending images, that would be helpful.
– AmirhosseinDZ
Nov 12 at 10:05












@AmirhosseinDZ sure => prntscr.com/lhaod7
– clusterBuddy
Nov 12 at 10:07




@AmirhosseinDZ sure => prntscr.com/lhaod7
– clusterBuddy
Nov 12 at 10:07












2 Answers
2






active

oldest

votes

















up vote
1
down vote













hasFile doesn't work because it's not a file. You already converted the file to base64 and post it as json so you would access the base64 as $request->input('image') - then, you have to do this: How to save a PNG image server-side, from a base64 data string



If you want to upload a file and access it on the server-side with hasFile, then you can do something like:






const fd = new FormData()
fd.append('image', this.file)
fd.append('type', this.name) // any other fields
// fd.append... more fields
axios.post('ooi', fd)

<b-form-file
:id="fileName"
v-model="file"
/>








share|improve this answer




























    up vote
    0
    down vote













    In your controller



    public function store(Request $request)
    {
    if($request->hasFile('image_file')){
    $file = $request->file('image_file');
    $name_image = time().$file->getClientOriginalName();
    $file->move(public_path().'/images/', $name_image);
    }
    }


    In your vue component



    <template>
    <form method="post" enctype="multipart/form-data">
    <div class="image">
    <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
    accept="image/*" class="input-file">
    <p v-if="!url">Click to browse your image</p>
    <img v-if="url" :src="url" class="img img-responsive full-width" />
    </div>
    </form>
    </template>
    <style>

    </style>
    <script>
    export default{
    data(){
    return{
    url: null
    }
    },

    methods: {
    onFileChange(e) {
    let files = e.target.files || e.dataTransfer.files;
    if (!files.length) {
    console.log('no files');
    }
    console.log(files);
    console.log(files[0]);
    const file = files[0];
    this.url = URL.createObjectURL(file);
    const formData = new FormData();
    formData.append('image_file', file, file.name);
    axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
    console.log(response.data)
    })
    .catch(function(err){
    console.log(err)
    });
    }
    }
    }
    </script>





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259138%2flaravel-vue-request-hasfileimage-returns-null%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      hasFile doesn't work because it's not a file. You already converted the file to base64 and post it as json so you would access the base64 as $request->input('image') - then, you have to do this: How to save a PNG image server-side, from a base64 data string



      If you want to upload a file and access it on the server-side with hasFile, then you can do something like:






      const fd = new FormData()
      fd.append('image', this.file)
      fd.append('type', this.name) // any other fields
      // fd.append... more fields
      axios.post('ooi', fd)

      <b-form-file
      :id="fileName"
      v-model="file"
      />








      share|improve this answer

























        up vote
        1
        down vote













        hasFile doesn't work because it's not a file. You already converted the file to base64 and post it as json so you would access the base64 as $request->input('image') - then, you have to do this: How to save a PNG image server-side, from a base64 data string



        If you want to upload a file and access it on the server-side with hasFile, then you can do something like:






        const fd = new FormData()
        fd.append('image', this.file)
        fd.append('type', this.name) // any other fields
        // fd.append... more fields
        axios.post('ooi', fd)

        <b-form-file
        :id="fileName"
        v-model="file"
        />








        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          hasFile doesn't work because it's not a file. You already converted the file to base64 and post it as json so you would access the base64 as $request->input('image') - then, you have to do this: How to save a PNG image server-side, from a base64 data string



          If you want to upload a file and access it on the server-side with hasFile, then you can do something like:






          const fd = new FormData()
          fd.append('image', this.file)
          fd.append('type', this.name) // any other fields
          // fd.append... more fields
          axios.post('ooi', fd)

          <b-form-file
          :id="fileName"
          v-model="file"
          />








          share|improve this answer












          hasFile doesn't work because it's not a file. You already converted the file to base64 and post it as json so you would access the base64 as $request->input('image') - then, you have to do this: How to save a PNG image server-side, from a base64 data string



          If you want to upload a file and access it on the server-side with hasFile, then you can do something like:






          const fd = new FormData()
          fd.append('image', this.file)
          fd.append('type', this.name) // any other fields
          // fd.append... more fields
          axios.post('ooi', fd)

          <b-form-file
          :id="fileName"
          v-model="file"
          />








          const fd = new FormData()
          fd.append('image', this.file)
          fd.append('type', this.name) // any other fields
          // fd.append... more fields
          axios.post('ooi', fd)

          <b-form-file
          :id="fileName"
          v-model="file"
          />





          const fd = new FormData()
          fd.append('image', this.file)
          fd.append('type', this.name) // any other fields
          // fd.append... more fields
          axios.post('ooi', fd)

          <b-form-file
          :id="fileName"
          v-model="file"
          />






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 4:44









          Noogen

          1,29299




          1,29299
























              up vote
              0
              down vote













              In your controller



              public function store(Request $request)
              {
              if($request->hasFile('image_file')){
              $file = $request->file('image_file');
              $name_image = time().$file->getClientOriginalName();
              $file->move(public_path().'/images/', $name_image);
              }
              }


              In your vue component



              <template>
              <form method="post" enctype="multipart/form-data">
              <div class="image">
              <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
              accept="image/*" class="input-file">
              <p v-if="!url">Click to browse your image</p>
              <img v-if="url" :src="url" class="img img-responsive full-width" />
              </div>
              </form>
              </template>
              <style>

              </style>
              <script>
              export default{
              data(){
              return{
              url: null
              }
              },

              methods: {
              onFileChange(e) {
              let files = e.target.files || e.dataTransfer.files;
              if (!files.length) {
              console.log('no files');
              }
              console.log(files);
              console.log(files[0]);
              const file = files[0];
              this.url = URL.createObjectURL(file);
              const formData = new FormData();
              formData.append('image_file', file, file.name);
              axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
              console.log(response.data)
              })
              .catch(function(err){
              console.log(err)
              });
              }
              }
              }
              </script>





              share|improve this answer



























                up vote
                0
                down vote













                In your controller



                public function store(Request $request)
                {
                if($request->hasFile('image_file')){
                $file = $request->file('image_file');
                $name_image = time().$file->getClientOriginalName();
                $file->move(public_path().'/images/', $name_image);
                }
                }


                In your vue component



                <template>
                <form method="post" enctype="multipart/form-data">
                <div class="image">
                <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
                accept="image/*" class="input-file">
                <p v-if="!url">Click to browse your image</p>
                <img v-if="url" :src="url" class="img img-responsive full-width" />
                </div>
                </form>
                </template>
                <style>

                </style>
                <script>
                export default{
                data(){
                return{
                url: null
                }
                },

                methods: {
                onFileChange(e) {
                let files = e.target.files || e.dataTransfer.files;
                if (!files.length) {
                console.log('no files');
                }
                console.log(files);
                console.log(files[0]);
                const file = files[0];
                this.url = URL.createObjectURL(file);
                const formData = new FormData();
                formData.append('image_file', file, file.name);
                axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
                console.log(response.data)
                })
                .catch(function(err){
                console.log(err)
                });
                }
                }
                }
                </script>





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In your controller



                  public function store(Request $request)
                  {
                  if($request->hasFile('image_file')){
                  $file = $request->file('image_file');
                  $name_image = time().$file->getClientOriginalName();
                  $file->move(public_path().'/images/', $name_image);
                  }
                  }


                  In your vue component



                  <template>
                  <form method="post" enctype="multipart/form-data">
                  <div class="image">
                  <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
                  accept="image/*" class="input-file">
                  <p v-if="!url">Click to browse your image</p>
                  <img v-if="url" :src="url" class="img img-responsive full-width" />
                  </div>
                  </form>
                  </template>
                  <style>

                  </style>
                  <script>
                  export default{
                  data(){
                  return{
                  url: null
                  }
                  },

                  methods: {
                  onFileChange(e) {
                  let files = e.target.files || e.dataTransfer.files;
                  if (!files.length) {
                  console.log('no files');
                  }
                  console.log(files);
                  console.log(files[0]);
                  const file = files[0];
                  this.url = URL.createObjectURL(file);
                  const formData = new FormData();
                  formData.append('image_file', file, file.name);
                  axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
                  console.log(response.data)
                  })
                  .catch(function(err){
                  console.log(err)
                  });
                  }
                  }
                  }
                  </script>





                  share|improve this answer














                  In your controller



                  public function store(Request $request)
                  {
                  if($request->hasFile('image_file')){
                  $file = $request->file('image_file');
                  $name_image = time().$file->getClientOriginalName();
                  $file->move(public_path().'/images/', $name_image);
                  }
                  }


                  In your vue component



                  <template>
                  <form method="post" enctype="multipart/form-data">
                  <div class="image">
                  <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
                  accept="image/*" class="input-file">
                  <p v-if="!url">Click to browse your image</p>
                  <img v-if="url" :src="url" class="img img-responsive full-width" />
                  </div>
                  </form>
                  </template>
                  <style>

                  </style>
                  <script>
                  export default{
                  data(){
                  return{
                  url: null
                  }
                  },

                  methods: {
                  onFileChange(e) {
                  let files = e.target.files || e.dataTransfer.files;
                  if (!files.length) {
                  console.log('no files');
                  }
                  console.log(files);
                  console.log(files[0]);
                  const file = files[0];
                  this.url = URL.createObjectURL(file);
                  const formData = new FormData();
                  formData.append('image_file', file, file.name);
                  axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
                  console.log(response.data)
                  })
                  .catch(function(err){
                  console.log(err)
                  });
                  }
                  }
                  }
                  </script>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 at 17:35

























                  answered Nov 21 at 22:14









                  Vladimir Salguero

                  1,7461128




                  1,7461128






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259138%2flaravel-vue-request-hasfileimage-returns-null%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Catalogne

                      Violoncelliste

                      Héron pourpré