Prevent browser from loading a drag-and-dropped file

前端 未结 10 1973
逝去的感伤
逝去的感伤 2020-11-28 02:12

I\'m adding an html5 drag and drop uploader to my page.

When a file is dropped into the upload area, everything works great.

However, if I accidentally drop

相关标签:
10条回答
  • 2020-11-28 02:15

    After a lot of fiddling around, I found this to be the stablest solution:

    var dropzoneId = "dropzone";
    
    window.addEventListener("dragenter", function(e) {
      if (e.target.id != dropzoneId) {
        e.preventDefault();
        e.dataTransfer.effectAllowed = "none";
        e.dataTransfer.dropEffect = "none";
      }
    }, false);
    
    window.addEventListener("dragover", function(e) {
      if (e.target.id != dropzoneId) {
        e.preventDefault();
        e.dataTransfer.effectAllowed = "none";
        e.dataTransfer.dropEffect = "none";
      }
    });
    
    window.addEventListener("drop", function(e) {
      if (e.target.id != dropzoneId) {
        e.preventDefault();
        e.dataTransfer.effectAllowed = "none";
        e.dataTransfer.dropEffect = "none";
      }
    });
    <div id="dropzone">...</div>

    Setting both effectAllow and dropEffect unconditionally on the window causes my drop zone not to accept any d-n-d any longer, regardless whether the properties are set new or not.

    0 讨论(0)
  • 2020-11-28 02:20

    I have an HTML object (embed) that fills the width and height of the page. The answer by @digital-plane works on normal web pages but not if the user drops onto an embedded object. So I needed a different solution.

    If we switch to using the event capture phase we can get the events before the embedded object receives them (notice the true value at the end of the event listener call):

    // document.body or window
    document.body.addEventListener("dragover", function(e){
      e = e || event;
      e.preventDefault();
      console.log("over true");
    }, true);
    
    document.body.addEventListener("drop", function(e){
      e = e || event;
      e.preventDefault();
      console.log("drop true");
    }, true);
    

    Using the following code (based on @digital-plane's answer) the page becomes a drag target, it prevents object embeds from capturing the events and then loads our images:

    document.body.addEventListener("dragover", function(e){
      e = e || event;
      e.preventDefault();
      console.log("over true");
    }, true);
    
    document.body.addEventListener("drop",function(e){
      e = e || event;
      e.preventDefault();
      console.log("Drop true");
    
      // begin loading image data to pass to our embed
      var droppedFiles = e.dataTransfer.files;
      var fileReaders = {};
      var files = {};
      var reader;
    
      for (var i = 0; i < droppedFiles.length; i++) {
        files[i] = droppedFiles[i]; // bc file is ref is overwritten
        console.log("File: " + files[i].name + " " + files[i].size);
        reader = new FileReader();
        reader.file = files[i]; // bc loadend event has no file ref
    
        reader.addEventListener("loadend", function (ev, loadedFile) {
          var fileObject = {};
          var currentReader = ev.target;
    
          loadedFile = currentReader.file;
          console.log("File loaded:" + loadedFile.name);
          fileObject.dataURI = currentReader.result;
          fileObject.name = loadedFile.name;
          fileObject.type = loadedFile.type;
          // call function on embed and pass file object
        });
    
        reader.readAsDataURL(files[i]);
      }
    
    }, true);
    

    Tested on Firefox on Mac.

    0 讨论(0)
  • 2020-11-28 02:21

    I am using a class selector for multiple upload areas so my solution took this less pure form

    Based on Axel Amthor's answer, with dependency on jQuery (aliased to $)

    _stopBrowserFromOpeningDragAndDropPDFFiles = function () {
    
            _preventDND = function(e) {
                if (!$(e.target).is($(_uploadBoxSelector))) {
                    e.preventDefault();
                    e.dataTransfer.effectAllowed = 'none';
                    e.dataTransfer.dropEffect = 'none';
                }
            };
    
            window.addEventListener('dragenter', function (e) {
                _preventDND(e);
            }, false);
    
            window.addEventListener('dragover', function (e) {
                _preventDND(e);
            });
    
            window.addEventListener('drop', function (e) {
                _preventDND(e);
            });
        },
    
    0 讨论(0)
  • 2020-11-28 02:27

    Note: Although the OP did not ask for an Angular solution, I came here looking for that. So this is to share what I found to be a viable solution, if you use Angular.

    In my experience this problem first arises when you add file drop functionality to a page. Therefore my opinion is that the component that adds this, should also be responsible for preventing drop outside of the drop zone.

    In my solution the drop zone is an input with a class, but any unambiguous selector works.

    import { Component, HostListener } from '@angular/core';
    //...
    
    @Component({
      template: `
        <form>
          <!-- ... -->
          <input type="file" class="dropzone" />
        </form>
      `
    })
    export class MyComponentWithDropTarget {
    
      //...
    
      @HostListener('document:dragover', ['$event'])
      @HostListener('drop', ['$event'])
      onDragDropFileVerifyZone(event) {
        if (event.target.matches('input.dropzone')) {
          // In drop zone. I don't want listeners later in event-chain to meddle in here
          event.stopPropagation();
        } else {
          // Outside of drop zone! Prevent default action, and do not show copy/move icon
          event.preventDefault();
          event.dataTransfer.effectAllowed = 'none';
          event.dataTransfer.dropEffect = 'none';
        }
      }
    }
    

    The listeners are added/removed automatically when component is created/destroyed, and other components using the same strategy on the same page do not interfere with each other due to the stopPropagation().

    0 讨论(0)
  • 2020-11-28 02:28

    For jQuery the correct answer will be:

    $(document).on({
        dragover: function() {
            return false;
        },
        drop: function() {
            return false;
        }
    });
    

    Here return false will behave as event.preventDefault() and event.stopPropagation().

    0 讨论(0)
  • 2020-11-28 02:30

    Preventing all drag and drop operations by default might not be what you want. It's possible to check if the drag source is an external file, at least in some browsers. I've included a function to check if the drag source is an external file in this StackOverflow answer.

    Modifying Digital Plane's answer, you could do something like this:

    function isDragSourceExternalFile() {
         // Defined here: 
         // https://stackoverflow.com/a/32044172/395461
    }
    
    window.addEventListener("dragover",function(e){
        e = e || event;
        var IsFile = isDragSourceExternalFile(e.originalEvent.dataTransfer);
        if (IsFile) e.preventDefault();
    },false);
    window.addEventListener("drop",function(e){
        e = e || event;
        var IsFile = isDragSourceExternalFile(e.originalEvent.dataTransfer);
        if (IsFile) e.preventDefault();
    },false);
    
    0 讨论(0)
提交回复
热议问题