2

请帮助我:

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

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

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

4

0 回答 0