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

151
Vistas
React accessing file data

In a React/Typescript app I have a component that accepts a file:

<div>
     <input type="file" {...register('avatar')} />
</div>

along with 2 string values. These values are all saved to FormData:

type FormData = {
  handle: string;
  role: string;
  avatar: File;
};

const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<FormData>();

I would like to access the avatar file name to use later on, as well as send the file to an AWS bucket like this:

const onSubmit = (data: FormData) => {
    console.log(data);
    console.log(data.avatar.name);
    ReactS3Client.uploadFile(data.avatar, data.handle + Date.now())
      .then((data) => console.log(data))
      .catch((err) => console.error(err));

There are a few problems happening here. First of all, when I console.log avatar.name it is simply blank. In one of my tests the file was Player.jpg, so I expected it to log "Player". Next, when I check my AWS bucket the file was indeed uploaded but when I try to open it I do not see an image and instead see: [object FileList.

Obviously somewhere in the process I am interacting with the uploaded file incorrectly. How can I access the name of the file and correctly upload it to AWS?

EDIT:

If I console.log(data.avatar); it gives me this:

avatar

If I console.log(data.avatar.name); which is an autofill option in VS Code then it simply prints undefined.

If I console.log(data.avatar[0].name); I get this error: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'File'. Property '0' does not exist on type 'File'.ts(7053).

How can I access that name property?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe that console.log(data.avatar[0].name); will give you what you need.

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