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

173
Visualizações
How do get the value of input element of type "file"

I have tried to solve this problem without success. I want to get the "value" of an input element of "type = file". I don't know what I am doing wrong that's preventing me from getting the "input value".

function addFile() {

  let toUpload = document.getElementById("fileid").click();
  console.log(toUpload.value);
  //Do something with file name;
}
<button class=" btn border border-1
  rounded-pill " id="add-btn" onclick="addFile()">
    <i class="bx bx-plus-medical">
    </i>
    Choose file                  
</button>

<input id='fileid' type='file' hidden/>

The file upload dialogue comes up. But I cannot access the file name. Who can figure out an issue with the above code please?

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

0

You have three different problems here.

  1. click() doesn't return the element, and the element has the value. Store the element in a variable and use that for reading and for clicking.
  2. You need to wait until the user picks a file before reading the value, currently you are trying to get the value before it has changed. Use an event listener.
  3. The value is an obfuscated path. Use the files property to get details of the files selected.

function addFile() {
  let toUpload = document.getElementById("fileid");
  toUpload.addEventListener('change', event => {
      //Do something with file name;
      console.log(toUpload.value);  
      console.log(toUpload.files[0].name);
  }, { once: true });
  toUpload.click();
}
<button class=" btn border border-1
  rounded-pill " id="add-btn" onclick="addFile()">
    <i class="bx bx-plus-medical">
    </i>
    Choose file                  
</button>

<input id='fileid' type='file' hidden/>

about 4 years ago · Juan Pablo Isaza Relatório

0

input type file doesn't have a value, it will have a property called files

files is an array of files (in case the input allows multiple), each file will have a property called name to get the display name

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