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

134
Visualizações
JS switch what form returns to a b64 string

I want to save an image in b64 format in my database. For reasons I want to deal with the logic on the frontend side of things. My backend sends a form and already encapsulates the filefield input with a onChange function that converts img -> b64:

<form>
    <input id="file-select" name="pic" class="form-control" type="file" onchange="getImage()">
    <button>submit</button>
</form>

where getImage() looks like this:

function getImage() {
    var reader = new FileReader();
    var f = document.getElementById("file-select").files;
    reader.onloadend = function () {
        console.log(reader.result);
    }
    reader.readAsDataURL(f[0]);
}

How can I replace what the input field submits on button press with the value of reader.readAsDataURL(f[0])?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

  • Use the input value property
  • Don't use inline on* JS handlers (same as hopefully you don't use inline style attributes). Use addEventListener() instead
  • Add a missing Form's action attribute for the URL you're sobmitting to
  • Add a required attribute to your Input Element

const EL = (sel, el) => (el || document).querySelector(sel);

const EL_file = EL("#file-select");

const readFile = () => {
  if (!EL_file.files) return;
  const FR = new FileReader();
  FR.addEventListener("load", (evt) => EL_file.value = evt.target.result);
  FR.readAsDataURL(this.files[0]);
};

EL_file.addEventListener("change", readFile);
<form id="file-form" action="save_base64.php">
  <input id="file-select" name="pic" class="form-control" type="file" required>
  <button>Submit</button>
</form>

about 4 years ago · Santiago Trujillo 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