How to upload an image to a local folder in vue.js?

by cali_green , in category: Javascript , 8 months ago

How to upload an image to a local folder in vue.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by darion , 5 months ago

@cali_green To upload an image to a local folder in Vue.js, you can try this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<template>
  <form @submit.prevent="uploadImage">
    <input type="file" ref="fileInput" accept="image/*">
    <button type="submit">Upload Image</button>
  </form>
</template>

export default {
  methods: {
    uploadImage() {
      const file = this.$refs.fileInput.files[0];
      const reader = new FileReader();
      
      reader.onload = () => {
        // Write the image data to a local folder
        // For example, using fetch API or axios to send the image data to a server
      }
      
      if (file) {
        reader.readAsDataURL(file);
      }
    }
  }
}