Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
How are image uploads done in Vue 3

I want to send text and an image to a blog post API endpoint that accept a text (required ) and an image ( optional ). I am able to POST a new blog post when I fill in a textarea only but unable to send an image in the POST body. Here's the form fields

<label for="comment" class="sr-only">Write what's on your mind</label>

<textarea
    v-model="form.post"
    rows="3"
    name="comment"
></textarea>

<label>
  <input ref="file" type="file" v-on:change="onImageUpload(e)" />
</label>

In my script setup

<script setup>
    import { ref } from "vue";
    
    const form = ref({
      post: null,
      file: null
    });
    
    function onImageUpload() {
        console.log(file.value.files);
        // form.value.file = form.value.file[0]; 
        // console.log("selected file", form.value);
    }
</script>

This code returns the error below

Uncaught ReferenceError: file is not defined

How can I access the image file being uploaded and pass it along inside the form.value?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

My way of handling file input is that I receive the file from onChange of the input. And store the file as a state in data This is how I implement.

  <input
        @change="handleFileInput"
        type="file"
        accept="image/jpeg, image/png"
  />

  data: () => ({
    file: null,
  }),

  methods: {
    handleFileInput(e) {
      this.file = e.target.files[0];
    }
  }

In your code, you tried to pass e from onChange, but you don't need to do this for just passing event object. Try adding a parameter that accept the event object and use that object to get access to chosen files. This might be different from your approach with refs, but it just works.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda