-1

I've gone through other similar threads and am not finding my answer, if I missed it, apologies for the duplicate.

I have a simple event handler for file submission with a form. It is as so:

onChange(e) {
    let files = e.target.files;
    console.log(files);
  }

The console log is:

FileList {0: File(692), length: 1}
0: File(692)
lastModified: 1546326376754
lastModifiedDate: Tue Jan 01 2019 00:06:16 GMT-0700 (Mountain Standard Time) {}
name: "userFile.txt"
size: 692
type: "text/plain"
webkitRelativePath: ""
__proto__: File
length: 1
__proto__: FileList

I would like to console.log(files.name) and get userFile.txt returned. I am getting undefined. Very sorry if this has been answered a million times, I've spent a long time searching and trying different dot notations etc. to no avail. Thanks

2

2 Answers 2

3

You can get it by files[0].name

function getFileName(e) {

  let files = e.target.files;
  console.log(files[0].name);
}
<input type='file' onchange='getFileName(event)'>

Sign up to request clarification or add additional context in comments.

1 Comment

lovely, will accept when in a few minutes when the site lets me. Thanks!
1

I believe you need to access the 0 property in files like so:

console.log(files[0].name);

And it should work.

Demonstration:

function getFileName(event) {

  const files = event.target.files;
  console.log(files[0].name);
}
<input type='file' oninput='getFileName(e)'>

1 Comment

No problem @HanleySoilsmith, glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.