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

154
Vistas
onDrop property in REACT HOOKS doesn't work on my DIV element loading BASE64 image

I have a form to upload images and store them in localStorage with base64, then display them on the page by calling the element. When I load the images alone by clicking on the label (Since the input has a display none by CSS) EVERYTHING works perfectly, the problem is when I do an onDrop, the console tells me the following errors:

Uncaught TypeError: Cannot read properties of undefined (reading '0')

if in the onDrop I remove the [0] from the e.target.files...I get the following error

Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

when the code is as follows:

 // LOAD IMAGE FUNCTION

    const handleImage = (files) => {
          const fileReader = new FileReader();
          fileReader.onloadend = () => {
            if (fileReader.readyState === 2) {
              setForm({ ...form, backdrop_path: fileReader.result });
            }
          };
        fileReader.readAsDataURL(files);
        setImage(true)
        return image 
    };
// This function submits the form, it is the button function

const onSubmit = (e) => {
        e.preventDefault();
        const { title, backdrop_path } = form;
        if (title === '' || backdrop_path === '') return;
        const movie = {
          title,
          backdrop_path,
        };
        localStorage.setItem('MyUploadedMovies', JSON.stringify(movie));
        setFormComplete(true)
      };

      //FUNCION REACT ONDROP
    const handleDragOver = (event) => {
      event.preventDefault()
    }

    const handleDrag = (evt) => {
      console.log(evt.currentTarget.id)
    } 
// This is the section that accepts the image in the form
 { image ? 
              <ProgressBar/>
              
              :
          <div className='fileImgContainer'
            draggable={true}
            onDragOver={handleDragOver}
            onDragStart={handleDrag}
            onDrop={(e) => handleImage(e.target.files[0])}
          >
            <label htmlFor="imgfiles" className='fileImgLabel'>
             Agregá un archivo o arrastralo y soltalo aquí
            </label>  
            <input
                className='fileImgInput'
                id='imgfiles'
                type='file'
                defaultValue=''
                accept='image/png,image/jpeg'
                onChange={(e) => handleImage(e.target.files[0])}
            />
          </div> 
      } 

I want to clarify that I am NOT using ANY library

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

const handleDrop = (ev) => {
  
  // Prevent default behavior (Prevent file from being opened)
  ev.preventDefault();

  if (ev.dataTransfer.items) {
    // Use DataTransferItemList interface to access the file(s)
    for (let i = 0; i < ev.dataTransfer.items.length; i++) {
      // If dropped items aren't files, reject them
      if (ev.dataTransfer.items[i].kind === 'file') {
        const file = ev.dataTransfer.items[i].getAsFile();
        handleImage(file)
      }
    }
  } else {
    // Use DataTransfer interface to access the file(s)
    for (let i = 0; i < ev.dataTransfer.files.length; i++) {
      handleImage(ev.dataTransfer.files[i])
    }
  }
}
about 4 years ago · Santiago Trujillo 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