1

I need to upload a file on drop event in browser, I need to identify whether the file is dragged from the same window or other window (may be from local drive). If the file is drag and dropped from the same window I don't need to show the upload popup but in IE it appears.Below is the code I used.

$("body").on('drop', function (e) {
        e.preventDefault();
        var files = e.originalEvent.dataTransfer.files;        
            if (files) {
                showMultipleUpload(files);
            }
            else {
                $.alert("ERROR");
            }        
});

I have tried the following methods but they didn't work

  1. e.srcElement, e.originalEvent.srcElement, e.target are having the dom in which i drop the file, no matter where it is dragged from.

  2. e.originalEvent.fromElement is null.

  3. Disable Mousedown for body.

    $("body").on('mousedown', function (e) { 
        e.preventDefault();
    });
    /* this worked but i can't perform any click event for the same body tag. */
    
  4. I had set the event bubbling parameter as false, unfortunately i can't upload any files. i.e. if i drop in to the body, event handler is not executed.

  5. Using flag to identify whether content is dragged from the same window.

     $("body").on('mousedown',function(e){
        e.preventDefault();
        flag = true; 
        /* if dragged from the same body tag it is set to true  */
     });
    
     $("body").on('drop', function (e) {
        e.preventDefault();
        if(!flag){
        var files = e.originalEvent.dataTransfer.files;        
            if (files) {
                showMultipleUpload(files);
            }
            else {
                $.alert("ERROR");
            } 
        }
        flag = false; 
        /* flag is cleared on every drop event, since we don't know 
           where the content is dragged from */
      });
    

This method works very well but if i drop the content outside the body, flag will be set to true always, so is there any legit way to solve this?

Community
  • 1
  • 1

0 Answers0