有了HTML5,老板再也不用担心我们的上传了,再加上有拖拽上传是不是很酷。百度一下,有关HTML5拖拽上传的文章和实例不少,都缺不了一个至关重要的东东DataTransfer。但是详细介绍的不多,尤其这个对象到底包含了哪些玩意。

翻墙出去问了下谷歌找到了DataTransfer的API,下面就介绍一下:

DataTransfer

拖拽数据传递对象,一般使用方式event.dataTransfer。

dataTransfer . dropEffect [ = value ]

拖拽效果,可选值:“none”, “copy”, “copyLink”, “copyMove”, “link”, “linkMove”, “move”, “all”, and “uninitialized”。

dataTransfer . items

拖拽的数据集合,是一个数组。

dataTransfer . setDragImage (element, x, y)

Uses the given element to update the drag feedback, replacing any previously specified feedback.

英文有点拗口,就是拖拽过程中定义一个元素替换原有的,可以看到拖拽元素跟随的效果。

dataTransfer . addElement (element)

Adds the given element to the list of elements used to render the drag feedback.

dataTransfer . types

Returns a DOMStringList listing the formats that were set in the dragstart event. In addition, if any files are being dragged, then one of the types will be the string “Files”.

data = dataTransfer . getData (format)

Returns the specified data. If there is no such data, returns the empty string.

获取自定义的数据格式,如ev.dataTransfer.getData("text");通常是配合ev.dataTransfer.setData使用。

dataTransfer . setData(format, data)

Adds the specified data.

添加自定义数据格式,如ev.dataTransfer.setData("text", ev.target.innerHTML);有点像jquery里面的data

dataTransfer . clearData( [ format ] )

Removes the data of the specified formats. Removes all data if the argument is omitted.

清除自定义的数据格式及其数据。

dataTransfer . files

Returns a FileList of the files being dragged, if any.

拖拽的文件列表对象。

下面是图片拖拽本地预览的DEMO:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>HTML5文件拖拽预览Demo</title>
<style type="text/css">
h1{
padding:0px;
margin:0px;
}
div#show{
border: 1px solid #ccc;
width: 400px;
height: 300px;
display: -moz-box;
display: -webkit-box;
-moz-box-align: center;
-webkit-box-align: center;
-moz-box-pack: center;
-webkit-box-pack: center;
resize:both;
overflow:auto;
}
div[id^=show]:hover{
border: 1px solid #333;
}
div#main{
width:100%;
}
div#successLabel
{
color:Red;
}
div#content
{
display:none;
}
</style>
<script type="text/javascript">
function init()
{
var dest = document.getElementById("show");
dest.addEventListener("dragover", function(ev)
{
ev.stopPropagation();
ev.preventDefault();
}, false); dest.addEventListener("dragend", function(ev)
{
ev.stopPropagation();
ev.preventDefault();
}, false); dest.addEventListener("drop", function (ev) {
ev.stopPropagation();
ev.preventDefault();
console.log(ev.dataTransfer)
var file = ev.dataTransfer.files[0];
var reader = new FileReader(); if (file.type.substr(0, 5) == "image") {
reader.onload = function (event) {
dest.style.background = 'url(' + event.target.result + ') no-repeat center';
dest.innerHTML = "";
};
reader.readAsDataURL(file);
}
else if (file.type.substr(0, 4) == "text") { reader.readAsText(file);
reader.onload = function (f) {
dest.innerHTML = "<pre>" + this.result + "</pre>";
dest.style.background = "white";
}
}
else {
dest.innerHTML = "暂不支持此类文件的预览";
dest.style.background = "white";
}
}, false);
} //设置页面属性,不执行默认处理(拒绝被拖放)
document.ondragover = function(e){e.preventDefault();};
document.ondrop = function(e){e.preventDefault();} window.onload=init;
</script>
</head>
<body>
<h1>HTML5文件拖拽预览Demo</h1>
<div id="show">
文件预览区,仅限图片和txt文件
</div>
</body>
</html>

HTML5拖拽功能中 dataTransfer对象详解的更多相关文章

  1. (转)javascript中event对象详解

    原文:http://jiajiale.iteye.com/blog/195906 javascript中event对象详解          博客分类: javaScript JavaScriptCS ...

  2. HTML5拖拽功能drag

    1.创建拖拽对象 给需要拖拽的元素设置draggable属性,它有三个值: true:元素可以被拖拽:false:元素不能被拖拽:auto: 浏览器自己判断元素是否能被拖拽. 2.处理拖拽事件当我们拖 ...

  3. django中request对象详解(转载)

    django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将  HttpRequest对象  作为第一个参数传入该函数. ...

  4. HTML5拖拽表格中单元格间的数据库

    效果图:

  5. 转:AJAX中xhr对象详解

    XJAX ,并不是一种新技术的诞生.它实际上代表的是几项技术按一定的方式组合在一在同共的协作中发挥各自的作用. 它包括: 使用XHTML和CSS标准化呈现: 使用DOM实现动态显示和交互: 使用XML ...

  6. Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示

    本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...

  7. 转-JS中document对象详解

    对象属性 document.title //设置文档标题等价于HTML的<title>标签 document.bgColor //设置页面背景色 document.fgColor //设置 ...

  8. JS中document对象详解

    转自:http://www.cnblogs.com/andycai/archive/2010/06/29/1767351.html 对象属性 document.title //设置文档标题等价于HTM ...

  9. JS中navigator对象详解

    <code class="language-html"><!doctype html> <html> <head> <meta ...

随机推荐

  1. js的事件机制

    js的事件机制 解释:当我们的行为动作满足了一定的条件后,会触发某事务的执行. 内容: 1.单双击事件 单击:onclick 当鼠标单击时候会触发 双击:ondbclick 当鼠标双击时候会触发 2. ...

  2. java类的基本结构

    对象依赖于类存在. 分析过程先有对象后有类,开发过程中先有类后有对象. new是为新建对象开辟内存空间的运算符:以类为模板,开辟空间实例化一个对象,并返回该对象的一个引用. 成员变量 直接在类中定义 ...

  3. leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)

    题目描述: In a given integer array nums, there is always exactly one largest element. Find whether the l ...

  4. 对drf序列化器的理解

    序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.(百度定义) 对应到drf中,序列化即把模型对象转换为字典形式, 再返回给前端,主要用于输出 反序列化: 把其他格式转化为程序中的格式. 对 ...

  5. Springboot第三篇:与前端fetch通信(关于前端传输json数据上传文件等等前后端的处理)

    关于前端接口传递的方法,推荐按以下使用: 若要在服务器上创建资源,推荐使用POST方法 若要检索某个资源,推荐使用GET方法 若要更新资源,推荐使用PUT方法 若要删除某个资源,推荐使用DELETE方 ...

  6. Angular material mat-icon 资源参考_Editor

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  7. Flask基础应用

    一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 Django: 优点: 大而全,组件非常全面. 缺点: 太大,加载太大,浪费资源. Flask: 优点: ...

  8. redis的主从同步

    一.redis的主从操作流程 1. 准备三个redis配置文件 #进入redis的配置文件夹,准备好这几个文件,6379不用管,默认的,和这次操作无关 [root@qishi ~]# cd /etc/ ...

  9. 再探php

    1. 如何打开一个php文件? 启动本地服务器和MySQL, 然后将php文件放在xampp -> htdocs  目录下(可以是子目录.孙子目录 ......),打开浏览器,在浏览器中输入 l ...

  10. selenium+Python(表单、多窗口切换)

    1.多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...