Disable File drag & drop if there isn't a file input

倖福魔咒の 提交于 2019-12-04 06:38:52

This document (documentation for a jQuery file upload plugin) shows how to disable the browser's default action: https://github.com/blueimp/jQuery-File-Upload/wiki/Drop-zone-effects

Quoting the document:

If you want to allow specific drop zones but disable the default browser action for file drops on the document, add the following JavaScript code:

$(document).bind('drop dragover', function (e) {
     e.preventDefault();
});

Note that preventing the default action for both the "drop" and "dragover" events is required to disable the default browser drop action.

This might not be relevant to the topic, but it is just reverse. If you don't want to drag-drop file in input file up-loader, you can use

$('body').on('drop', function (e) {
     return false;
});

It worked for me in both IE 11 and chrome. Thanks Francois for your similar guidelines

Luis Castro

the dropzone must be inside the form without any HTML elements:

<form id="frmdropzone" class="dropzone" action="Save" method="post" enctype="multipart/form-data">
       <div class="dropzone-previews"></div>
</form>

To improve on Jan's answer, if you don't want to disable the drag & drop on file input elements:

$(document).bind("drop dragover", function(e){
    if(e.target.type != "file"){
        e.preventDefault();
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!