Categories
JavaScript Answers

How to remove a file from the FileList with JavaScript?

Spread the love

Sometimes, we want to remove a file from the FileList with JavaScript.

In this article, we’ll look at how to remove a file from the FileList with JavaScript.

How to remove a file from the FileList with JavaScript?

To remove a file from the FileList with JavaScript, we can create an array from the FileList object and then remove the entries from the array.

For instance, we write

const input = document.getElementById("files");
const fileListArr = Array.from(input.files);
fileListArr.splice(index, 1);
console.log(fileListArr);

to select the file input with getElementByIf.

Then we get the files selected from the file input with input.files.

And we convert it to an array with Array.from.

Then we call splice with the index of the file we want to remove and 1 to remove the file from the fileListArr array.

Conclusion

To remove a file from the FileList with JavaScript, we can create an array from the FileList object and then remove the entries from the array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *