简体   繁体   English

Chrome拖放文件/文件夹不起作用

[英]Chrome drag drop file/folder not working

I am trying to create PoC for Drag and drop a folder onto Chrome (v46) with the below code but alert is not getting triggered. 我正在尝试使用以下代码创建PoC,以将文件夹拖放到Chrome(v46)上,但不会触发alert Chrome switches to the folder browser view when a folder is dropped or opens the dragged file as it is dropped. 放置文件夹时,Chrome切换到文件夹浏览器视图,或者在放置时打开打开的文件。 Basically it keeps the default behavior. 基本上,它保留默认行为。 I tried to open the html file like " http://localhost/index.html " and "file:///C:\\index.html" but both behave the same. 我试图打开类似“ http://localhost/index.html ”和“ file:/// C:\\ index.html”的html文件,但是两者的行为相同。

Where am I going wrong? 我要去哪里错了?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Drop File/Folder</title>
</head>
<body>
    <div id="dropzone" style="border: solid 1px; padding: 200px;">Drop files or folders here</div>

    <script type="text/javascript">
        var dropzone = document.getElementById('dropzone');

        dropzone.ondrop = function (e) {
            alert("dropped!");
            e.preventDefault();
        };

        // I also tried this but no success
        //dropzone.addEventListener('drop', function (e) {
        //    alert("dropped!");
        //    e.preventDefault();
        //});
    </script>
</body>
</html>

It turns out you need to add a dragover handler. 事实证明,您需要添加一个拖动处理程序。

dropzone.ondragover = function (e) {
    e.preventDefault();
};

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

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