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

733
Vistas
Need image in the form of Byte Array in React JS

Problem Statement:-

I have written the code to upload an image in React JS. However, I need to send the image data to server and database that's why I need the image data in Byte Array. How can I get the uploaded image data in byte array?

Any help regarding this issue is highly appreciated.

Code:-

import React, { useState } from "react";
import "./style.css";

const ImageUpload = () => {
    
  const [file, setFile] = useState(null);
  const [flag, setFlag] = useState(false);

  const fileChangedHandler = (event) => {
    let file = event.target.files[0];
    let reader = new FileReader();

    console.log(file);
    reader.onload = function (e) {
      setFile(e.target.result);
      setFlag(true)
    };
    reader.readAsDataURL(event.target.files[0]);


  };
  return (
    <div id="modeling">
      <div className="container">
        <div className="row">
          <div className="col">
            <div className="modeling-text">
            </div>
          </div>
        </div>
       
       {flag ?  <img
          src={file}
          alt={""}
          width="190"
          height="220"
          text-align="left"
          style={{ display: "block" }}
        />: <img 
        src="./Image_Placeholder.jpeg"
          alt={""}
          width="190"
          height="220"
          text-align="left"
          style={{ display: "block" }}/>}
        
      </div>
      <div className="input-group mt-1 offset-1">
                <div className="custom-file smaller-input">
                  <input
                    type="file"
                    className="custom-file-input"
                    name="file"
                    inputprops={{ accept: "image/*" }}
                    accept=".png,.jpg,.jpeg"
                    onChange={fileChangedHandler}
                    id="inputGroupFile01"
                  />
                  
                 <label className="custom-file-label" htmlFor="inputGroupFile01">  Choose your image
                  </label>
                  <i className="bi bi-cloud-upload"></i>
                </div>
              </div>
    </div>
  );
};

export default ImageUpload;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could use FormData to send your file to the server.

const formData = new FormData()
if (file) {
  formData.append('file', file, file.name)
}

axios.post(`/upload`, formData, {
  headers: {
    'Content-Type': 'multipart/form-data',
  },
})
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