合并/合并文件列表 [英] Merging/combining Filelist
问题描述
我有两个包含相同格式的多个文件的Filelist对象,我想将其合并.
I've got two Filelist objects containing multiple files in a same form and I want to merge it.
var data1 = $('#one')[0].files;
var data2 = $('#two')[0].files;
console.log(data1);
console.log(data2);
var obj = $.merge(data1,data2);
console.log(obj);
我尝试了$.merge
和$.extend
,结果obj似乎具有所有文件,但是长度不正确:
I tried $.merge
and $.extend
, the result obj seems have all the files, but the length of it is incorrect:
FileList {2: File, 3: File, 0: File, 1: File, length: 2}
jsFiddle: https://jsfiddle.net/nxtdnhgu/
jsFiddle: https://jsfiddle.net/nxtdnhgu/
推荐答案
来自jquery文档.
From jquery documentation.
$.merge()函数具有破坏性.它将第一个对象的长度和数字索引属性更改为包括第二个对象的项.
The $.merge() function is destructive. It alters the length and numeric index properties of the first object to include items from the second.
如果需要原始的第一个数组,请在调用$ .merge()之前对其进行复制.
If you need the original first array, make a copy of it before calling $.merge().
var obj = $.merge( $.merge( [], data1 ), data2 );
正在工作的js小提琴: https://jsfiddle.net/t1n08ykd/
Working js fiddle: https://jsfiddle.net/t1n08ykd/
这篇关于合并/合并文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!