How to set File objects and length property at FileList object where the files are also reflected at FormData object? How to set File objects and length property at FileList object where the files are also reflected at FormData object? javascript javascript

How to set File objects and length property at FileList object where the files are also reflected at FormData object?


Edit:

As proven by OP, in one of their gist, there is actually a way to do it...

The DataTransfer constructor (currently only supported by Blink, and FF >= 62), should create a mutable FileList (chrome currently always return a new FileList, but it doesn't really matter for us), accessible through the DataTransferItemList.

If I'm not mistaken, this is currently the only specs-wise way to do it, but Firefox had a bug in their implementation of the ClipboardEvent constructor, where the same DataTransferItemList was and set to the mode read/write which allowed a workaround for FF < 62. I am not sure of my interpretation of the specs, but I believe it should not be accessible normally).

So the way guest271314 found to set arbitrary files on a FileList is as follows:

const dT = new DataTransfer();dT.items.add(new File(['foo'], 'programmatically_created.txt'));inp.files = dT.files;
<input type="file" id="inp">