How to work with FileList (from <input type=“file”>) in Javascript?

前端 未结 3 819
心在旅途
心在旅途 2020-12-11 07:24

In this W3schools example, console.log on the input element reveals a FileInput object:

FileList {0: File, 1: File, length: 2}

3条回答
  •  星月不相逢
    2020-12-11 08:04

    It is not possible to add File objects to FileList. You can use FormData to append Files to a single object.

    var data = new FormData();
    document.querySelector("input[type=file]")
    .addEventListener("change", function(event) {
      for (var i = 0, files = event.target.files; i < files.length; i++) {
        data.append("file-" + [...data.keys()].length, files[i], files[i].name)
      }
    })
    

提交回复
热议问题