Prevent browser from loading a drag-and-dropped file

前端 未结 10 2013
逝去的感伤
逝去的感伤 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: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);
            });
        },
    

提交回复
热议问题