Is it possible to update FileList?

后端 未结 2 1122
广开言路
广开言路 2020-11-28 10:20

I have:


Every time the user selects a file(s), I build a list o

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 10:52

    You can't modify a Filelist, but you can create a new one using a DataTransfer object, and if you wish you can copy your data into it to create a duplicate with the specific change you want to make.

    let list = new DataTransfer();
    let file = new File(["content"], "filename.jpg");
    list.items.add(file);
    
    let myFileList = list.files;
    

    You can then set it as the file attribute of the DOM node:

    fileInput.files = myFileList;
    

    If you wished, you could iterate over your old FileList, copying files to the new one.

提交回复
热议问题