Merging/combining Filelist

限于喜欢 提交于 2019-12-13 02:04:14

问题


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);

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/


回答1:


From jquery documentation.

The $.merge() function is destructive. It alters the length and numeric index properties of the first object to include items from the second.

If you need the original first array, make a copy of it before calling $.merge().

var obj = $.merge( $.merge( [], data1 ), data2 );

Working js fiddle: https://jsfiddle.net/t1n08ykd/



来源:https://stackoverflow.com/questions/30593963/merging-combining-filelist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!