Home >Web Front-end >JS Tutorial >How Can I Modify File Objects and FileList Length within FormData?
How can we edit File objects and adjust the length property of the FileList within a FormData object?
FileList objects typically have a Symbol.iterator property, allowing us to set objects that are iterable as File types. However, the files .length property remains at 0.
A recent breakthrough has emerged, as demonstrated by the OP in their gist. Utilizing the DataTransfer constructor, we can create a mutable FileList accessible via DataTransferItemList.
In Blink browsers and Firefox versions 62 and above, the DataTransfer constructor can create a mutable FileList. Prior to Firefox 62, a bug in the implementation of ClipboardEvent provided a workaround.
Here's an example of how to achieve this:
const dT = new DataTransfer(); dT.items.add(new File(['foo'], 'programmatically_created.txt')); inp.files = dT.files;
<input type="file">
This approach effectively modifies the file objects and sets the length property of the FileList correctly, allowing them to be reflected in the FormData object.
The above is the detailed content of How Can I Modify File Objects and FileList Length within FormData?. For more information, please follow other related articles on the PHP Chinese website!