如何在FileList中添加文件

请帮帮我:

更新: 在控制台日志中,它显示为合并但长度仍显示最后拖动的长度

我正在使用up_files = event.originalEvent.dataTransfer.files; 捕获在div中拖放的文件。

当用户第一次拖动文件时,所有文件都移动到up_files ,第二次如果再次将文件放入div中,我怎样才能将这些文件推送到现有数组列表而不是清除数组并插入upfiles

 #fileToUpload is a input file id #total is div id 

示例代码:

 $( '#total' ).bind( 'dragover',function(event) { event.stopPropagation(); event.preventDefault(); }); $( '#total' ).bind( 'drop',function(event) { event.stopPropagation(); event.preventDefault(); if( upfiles == 0 ) { upfiles = event.originalEvent.dataTransfer.files; upfiles = Array.prototype.slice.call(upfiles, 0); } else { //push the file here } $( "#fileToUpload" ).trigger( 'change' ); }); 

编辑:这是改变:

 $( "#fileToUpload" ).change( function() { $('#total').css("background-image",""); var fileIn = $( "#fileToUpload" )[0]; if(!upfiles) { upfiles = fileIn.files; upfiles = Array.prototype.slice.call(upfiles, 0); } else { if(fileIn.files.length > 0 ) { if( confirm( "Drop: Do you want to clear files selected already?" ) == true ) { upfiles = fileIn.files; upfiles = Array.prototype.slice.call(upfiles, 0); } else return; } //DISPLAYING FILE HERE USING EACH LOOP } }); 

更新 :

我试过这样的:

 $( '#total' ).bind( 'drop',function(event) { event.stopPropagation(); event.preventDefault(); alert("hello"+upfiles_new); if( upfiles_new == 0 ) { upfiles_new = event.originalEvent.dataTransfer.files; console.dir(upfiles_new); upfiles = Array.prototype.slice.call(upfiles_new, 0); } else { alert("hello world"); // console.dir(upfiles_new); upfiles_temp = event.originalEvent.dataTransfer.files; var upfiles = $.merge(upfiles_temp, upfiles_new) console.dir(upfiles); // in console log it is merged upfiles = Array.prototype.slice.call(upfiles, 0); // but in this array it is not showing alert(print_r(upfiles)); console.dir(upfiles); } $( "#fileToUpload" ).trigger( 'change' ); }); 

我认为我更接近..任何人都可以建议任何修改