-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.html
59 lines (51 loc) · 1.22 KB
/
file.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--
* @Description:
* @Author: zhangchuangye
* @Date: 2018-09-08 15:08:08
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>drag file</title>
</head>
<style>
*{
margin:0;
padding:0;
}
.box1{
width: 300px;
height: 300px;
display: inline-block;
border:1px solid #eee;
}
</style>
<body>
<h3>利用h5 drop readAsDataURL实现拖拽到页面图片预览</h3>
<div id='div1' class="box1">
</div>
<script>
var targetEle=document.getElementById('div1')
targetEle.ondragover=function(ev){
console.log('over')
ev.preventDefault()
}
targetEle.ondrop=function(ev){
ev.preventDefault()
console.log('drop')
var img=new Image()
var file=ev.dataTransfer.files[0]
console.log('file',file)
var fs=new FileReader()
fs.readAsDataURL(file)
fs.onload=function (){
img.src=fs.result
}
ev.target.appendChild(img)
}
</script>
</body>
</html>