Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hash's avatar
Level 1

Image Upload on button click.

Hi, I am trying to upload an image using vue js inline template on blade. I am uploading an image, it works fine but the image upload as soon as i select the image, I want to click on a button then only the image uploads. below is my code m using:

Vue method

         uploadImage(event) {

            const URL = 'api/v1/chats/image';

            let data = new FormData();
            data.append('name', 'my-picture');
            data.append('file', event.target.files[0]);
            data.append('chatid', this.chat);
            data.append('senderid', this.auth_id);

            let config = {
                header: {
                    'Content-Type': 'image/png'
                }
            };
      
            axios.post(URL, data, config).then(
                response => {
                    console.log('image upload response > ', response)
                  }
              )
          },

Blade

        <input type="file" accept="image/*" @change="uploadImage($event)" 
                                     id="file-input">

as soon as i upload an image it calls the api as my api is inside the method. but how do i call the api with my image on button click?. would really appreciate if someone could help. Thanks

0 likes
5 replies
bobbybouwmann's avatar

You can assign the same method to the click on a button

<input type="file" accept="image/*" v-model="image>
<button @click="uploadImage">Upload image</button>
Hash's avatar
Level 1

@bobbybouwmann if I use v-model on input it gives me a template error, and if i try to call the method using a button it throws me an error saying:

Uncaught TypeError: Cannot read property '0' of undefined at VueComponent.uploadImage

Hash's avatar
Level 1

@arifkhn46 thanks for the links, I managed to solved it by having ideas from the links you provided. appreciated :))

1 like

Please or to participate in this conversation.