博客
关于我
Web API - File - Selecting files using drag and drop
阅读量:312 次
发布时间:2019-03-03

本文共 1571 字,大约阅读时间需要 5 分钟。

文章目录

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.

拖曳选择文件

在这里插入图片描述

            
Selecting files using drag and drop

参考

转载地址:http://vhyq.baihongyu.com/

你可能感兴趣的文章
Navicat 设置时间默认值(当前最新时间)
查看>>
navicat 连接远程mysql
查看>>
navicat:2013-Lost connection to MySQL server at ‘reading initial communication packet解决方法
查看>>
Navicate for mysql 数据库设计-数据库分析
查看>>
Navicat下载和破解以及使用
查看>>
Navicat中怎样将SQLServer的表复制到MySql中
查看>>
navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题
查看>>
Navicat可视化界面导入SQL文件生成数据库表
查看>>
Navicat向sqlserver中插入数据时提示:当 IDENTITY_INSERT 设置为 OFF 时,不能向表中的标识列插入显式值
查看>>
Navicat因导入的sql文件中时间数据类型有参数而报错的原因(例:datetime(3))
查看>>
Navicat如何连接MySQL
查看>>
navicat导入.sql文件出错2006- MySQLserver has gone away
查看>>
Navicat导入海量Excel数据到数据库(简易介绍)
查看>>
Navicat工具Oracle数据库复制 or 备用、恢复功能(评论都在谈论需要教)
查看>>
Navicat工具中建立数据库索引
查看>>
navicat工具查看MySQL数据库_表占用容量_占用空间是多少MB---Linux工作笔记048
查看>>
navicat怎么导出和导入数据表
查看>>
Navicat怎样同步两个数据库中的表
查看>>
Navicat怎样筛选数据
查看>>
Navicat报错connection is being used
查看>>