@filiberto
To upload images in Vue.js with Axios, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<template> <div> <input type="file" @change="onFileChange"> <button @click="uploadImage">Upload</button> </div> </template> <script> export default { data() { return { file: null } }, methods: { onFileChange(e) { this.file = e.target.files[0]; }, uploadImage() { let formData = new FormData(); formData.append('image', this.file); this.axios.post('http://your-api-endpoint', formData) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); } } } </script> |
Make sure you have Axios installed in your project before using it in your Vue component. You can install Axios with the following command:
1
|
npm install axios --save |
That's it! You should now be able to upload images in Vue.js using Axios.