【问题标题】:Is it possible to combine two FileList objects?是否可以组合两个 FileList 对象?
【发布时间】:2015-07-18 15:11:23
【问题描述】:

我有一个 FileList 对象,其中包含以前上传的文档。我正在尝试使用另一个函数通过使用另一个 FileList 对象将更多文件添加到此集合中,因此我需要将辅助 FileList 对象“附加”到主要对象上。怎么可能做到这一点?

【问题讨论】:

  • 我认为这个问题也可以回答添加或推送新文件到 FileList 对象的问题,所以我将对其进行编辑以同时回答该问题

标签: javascript object filelist


【解决方案1】:

您必须先将FileList 对象转换为数组,然后您可以简单地concat 多个数组。

const joined = Array.from(fileListA).concat(Array.from(fileListB));

【讨论】:

    【解决方案2】:
    const filesArray = [...filesList1, ...filesList2];
    console.log(filesArray) // [File, File, File, File, File, ...]
    

    【讨论】:

      【解决方案3】:

      所选答案完全阐明了路径。这里也可以使用临时变量,如下所示:

       var temp = files
       files=Array.prototype.slice.call(temp).concat(Array.prototype.slice.call(event.target.files))
      

      同样在 React 中用于设置文件的状态,可以在 handleInputChange 函数中使用以下代码:

       var temp = this.state.files;         
       this.setState({
              files: Array.prototype.slice.call(temp).concat(Array.prototype.slice.call(event.target.files))
         })
      

      【讨论】:

        【解决方案4】:
        var fileLists = [fileListA, fileListB];
        var files = fileLists.reduce(function(a, c) {
            return Array.prototype.concat.call(Object.values(a), Object.values(c))
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-29
          • 2017-08-01
          • 2010-10-08
          • 1970-01-01
          • 1970-01-01
          • 2014-10-09
          • 2022-01-05
          相关资源
          最近更新 更多