简体   繁体   English

jQuery 拖放 - 只允许从浏览器外部拖动

[英]jQuery Drag & Drop - only allow drags from outside the browser

I am currently working on a website that uses the drag-and-drop functions of modern browsers in order to upload a file to a server with an ajax-call.我目前正在开发一个网站,该网站使用现代浏览器的拖放功能,以便通过 ajax 调用将文件上传到服务器。

I am using the jQuery-Library and bind the appropriate events.我正在使用 jQuery-Library 并绑定适当的事件。 There are a lot of them: dragstart, dragenter, dragover, dragleave etc...它们有很多:dragstart、dragenter、dragover、dragleave 等...

I was wondering if I could only allow dragging files from "outside the browser".我想知道我是否只能允许从“浏览器外部”拖动文件。 At the moment I have the problem that dom-elements are also draggable to my drop-area.目前我遇到的问题是 dom 元素也可以拖动到我的放置区域。

Is there any technical method to distinguish between external drags (outside -> browser) and internal drags (dom-element to dom-element)?有没有什么技术方法可以区分外部拖动(外部 -> 浏览器)和内部拖动(dom-element 到 dom-element)?

你可以换个思路,浏览器拖拽文件有后缀,拖拽后可以判断一个文件名是否有后缀。

At my case, I found a solution, using the html5 attributes changed to react I could filter only the type I needed...在我的情况下,我找到了一个解决方案,使用更改为响应的 html5 属性我只能过滤我需要的类型......

function checkDrag(event) {    
  if (event.dataTransfer.types[0] === 'Files') {
    //what you want do to
  }    
}

<Wrapper
  className="wrapper-body"
  onDragEnter={event => checkDrag(event)}
  ondragover={event => checkDrag(event)}
>
{Content} 
<Wrapper>

at my case, I worked only with start event but you can use the others html5 api methods to fill your needs, I guess :)就我而言,我只使用 start 事件,但您可以使用其他 html5 api 方法来满足您的需求,我猜:)

This link helped me a lot https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations#droptargets这个链接帮了我很多https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations#droptargets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM