How to get a file name

Asked

Viewed 40 times

-1

I’m creating a form and I want to get his name. I already used filereader, tried to use it but could not. I did it in a way that shows the file data in an array, but I don’t know how to get the name of this array

<script>
    const fileSelector = document.getElementById('images');
    fileSelector.addEventListener('change', (event) => {
        const fileList = event.target.files;
        console.log(fileList);
    });
</script>

1 answer

1


The estate files of a <input> of the kind file returns a FileList, which is an object similar to an array. It is therefore a list of files (instances of File).

To get the file name of an object File, you can use the property name. For other properties see the documentation.

An example:

const field = document.querySelector('#field');

field.addEventListener('change', (event) => {
  for (const file of field.files) {
    console.log(file.name);
  }
});
<input type="file" id="field" />

  • 1

    Thank you, you helped me a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.