admin管理员组文章数量:1221920
I think the solution to this is staring me in the face, but I can't seem to find it.
So, I'm making a site that has a small file upload section. The upload section will have a place to upload an image for different devices and device orientations (i.e. iPhone, iPad, portrait and landscape). I can easily figure out how to implement the drag & drop on the individual device icons, but if the user misses the drop area, the browser will just navigate to that image on their system. How do I disable drag & drop if there isn't a file input type to receive the dragged file?
I think the solution to this is staring me in the face, but I can't seem to find it.
So, I'm making a site that has a small file upload section. The upload section will have a place to upload an image for different devices and device orientations (i.e. iPhone, iPad, portrait and landscape). I can easily figure out how to implement the drag & drop on the individual device icons, but if the user misses the drop area, the browser will just navigate to that image on their system. How do I disable drag & drop if there isn't a file input type to receive the dragged file?
Share Improve this question asked Oct 26, 2012 at 6:55 CJT3CJT3 2,9087 gold badges33 silver badges45 bronze badges 1- listen to drag&drop events on the whole page, reject and prevent default? – John Dvorak Commented Oct 26, 2012 at 7:08
4 Answers
Reset to default 15This 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
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();
}
});
本文标签: javascriptDisable File drag amp drop if there isn39t a file inputStack Overflow
版权声明:本文标题:javascript - Disable File drag & drop if there isn't a file input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739295154a2156871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论