Web API - File - Selecting files using drag and drop

Selecting files using drag and drop

You can also let the user drag and drop files into your web application.

The first step is to establish a drop zone. Exactly what part of your content will accept drops may vary depending on the design of your application, but making an element receive drop events is easy:

let dropbox;

dropbox = document.getElementById("dropbox");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);

In this example, we’re turning the element with the ID dropbox into our drop zone. This is done by adding listeners for the dragenter, dragover, and drop events.

We don’t actually need to do anything with the dragenter and dragover events in our case, so these functions are both simple. They just stop propagation of the event and prevent the default action from occurring:

function dragenter(e) {
  e.stopPropagation();
  e.preventDefault();
}

function dragover(e) {
  e.stopPropagation();
  e.preventDefault();
}

The real magic happens in the drop() function:

function drop(e) {
  e.stopPropagation();
  e.preventDefault();

  const dt = e.dataTransfer;
  const files = dt.files;

  handleFiles(files);
}

Here, we retrieve the dataTransfer field from the event, pull the file list out of it, and then pass that to handleFiles(). From this point on, handling the files is the same whether the user used the input element or drag and drop.

拖曳选择文件

在这里插入图片描述

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Selecting files using drag and drop</title>
        <style type="text/css">
            #dropbox {
                width: 100px;
                height: 100px;
                box-sizing: border-box;
                border: 1px solid #000000;
            }
        </style>
    </head>
    <body>
        <div id="dropbox"></div>

        <script type="text/javascript">
            let dropbox = document.getElementById("dropbox");

            dropbox.addEventListener("dragenter", function(e) {
                e.stopPropagation();
                e.preventDefault();
            }, false);

            dropbox.addEventListener("dragover", function(e) {
                e.stopPropagation();
                e.preventDefault();
            }, false);

            dropbox.addEventListener("drop", function(e) {
                e.stopPropagation();
                e.preventDefault();

                const dataTransfer = e.dataTransfer;
                const files = dataTransfer.files;

                console.log(dataTransfer)
                console.log(files);
            }, false);
        </script>
    </body>
</html>

参考

Selecting files using drag and drop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值