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

本文共 1574 字,大约阅读时间需要 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/

你可能感兴趣的文章
从零构建通讯器--5.2三次握手,telnet,wireshark
查看>>
如何判断两个浮点数是否相等?
查看>>
2021牛客寒假算法基础集训营3
查看>>
苹果进军搜索,背后藏着什么“阳谋”?
查看>>
egg:如何在控制器中拿到前端传的参数
查看>>
MVC之修改
查看>>
struct 模块
查看>>
python之集合类型内置方法
查看>>
编程与编程语言分类
查看>>
【 UVA - 572 】 Oil Deposits (DFS水题)
查看>>
约瑟夫环问题
查看>>
CF #716 (Div. 2) B. AND 0, Sum Big(思维+数学)
查看>>
Java 設計模式 - 建造者模式
查看>>
ES6 JavaScript 重新認識 Promise
查看>>
分享九款不同页面404源码html
查看>>
404页圈小猫游戏代码
查看>>
好看清新卡通人物404单页网站源码
查看>>
简洁仿t猫404页html源码
查看>>
Python九齿耙(Ninerake)数据采集大数据深度学习智能分析爬虫软件的正则表达式规则简介
查看>>
Kotlin实现冒泡排序
查看>>