Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

370
Vistas
I can't upload image files from .vue to my database

I'm using Vue with Laravel and I tried to do a simple crud which is working good regarding things like title or description of an article (text-fields) but when I try to send an image file, it doesn't work. I have tried formData but to no avail.

This is my form in template, title goes in the database with no problem, if I console log selectedFile then it shows the file selected correctly but the addArticle method not attaching the images

<form @submit.prevent="addArticle" class="container">
      <label>Title</label>
      <input type="text" v-model="article.title" />
      <label>Image</label>
      <input type="file"  v-on:change="selectedFile"  />
      <button type="submit">Create</button>
</form>

This is my script

<script>
export default { 
  data() {
    return {
      fileSelected: null,       
      article: {},
    };
  },

  methods: {
    addArticle() {
       var formData =new FormData();
       formData.append(this.article.image, this.fileSelected);
        
      axios
        .post("http://localhost:8000/api/articles", this.article) 
        .then((response) => this.$router.push({ name: "ArticlesList" }))
        .catch((err) => console.log(err))
        .finally(() => (this.loading = false));
      }
    ,

    selectedFile(event) {
      this.fileSelected = event.target.files[0];
    },
  },
};
</script>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

this is my code

<input type="file" @change="onFileChange">


  onFileChange(e) {
    this.sendphoto = e.target.files[0];

  },

  editUser() {
    var self = this;
    let formData = new FormData();
   
      formData.append("image" , self.sendphoto )
  
    let config = {
      header : {
        'Content-Type' : 'multipart/form-data'
      }
    }
    axios.post('/edit-user' , formData , config)
            .then(function (response) {

            })
            .catch(function (error) {
              console.log(error);
            })
  },
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are creating a FormData object but you are not sending it within your Axios request. In order to send the file and form data, you have to append everything to FormData object.

<script>
export default { 
  data() {
    return {
      fileSelected: null,       
      article: {},
    };
  },

  methods: {
    addArticle() {
      var formData =new FormData();
      formData.append('image', this.fileSelected);
      formData.append('title', this.article.title);
        
      axios
        .post("http://localhost:8000/api/articles", formData) 
        .then((response) => this.$router.push({ name: "ArticlesList" }))
        .catch((err) => console.log(err))
        .finally(() => (this.loading = false));
      }
    ,

    selectedFile(event) {
      this.fileSelected = event.target.files[0];
    },
  },
};
</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

this worked in sending image files as string on database, hopefully it helps other people that are having similar problems

setup() {
        const base64 = ref()
        const changeFile= async(event) => {
            const file = event.target.files[0]; 
            base64.value = await convertBase64(file);          
        }    
 
        const convertBase64 = (file) => {
            return new Promise ((resolve, reject) => {
                const fileReader = new FileReader();
                fileReader.readAsDataURL(file);

                fileReader.onload = () => {
                    resolve(fileReader.result)
                }

                fileReader.onerror = (error) => {
                    reject(error)
                }
            })
        } 
  
        const form = reactive({
            title: " ",
            body: " ",
            image:  base64,
        })
     
        const submitForm = () => {            
            axios.post("http://localhost:8000/api/articles", form)
        }
        return { changeFile, submitForm, form}
    },

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda