-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.html
145 lines (120 loc) · 4.03 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<html>
<head>
<style>
a {
color: #369;
}
#drop-area {
border: 2px dashed #ccc;
border-radius: 20px;
margin: 10px auto;
padding: 20px;
min-height: 200px;
}
#drop-area.highlight {
border-color: purple;
}
#fileElem {
display: none;
}
#progress {
display: none;
position: fixed;
width: 100%;
height: 20px;
padding-top: 1px;
margin-top: 0px;
border: 1px #0064B4;
background-color: #e6e6e6;
color: #0064B4;
}
</style>
</head>
<body>
<progress id="progress" value="0" max="100"></progress>
<div id="drop-area">
<pre>
{{range $href, $name := . }}
<a href="{{$href}}">{{$name}}</a>
{{ end}}
</pre>
<input type="file" id="fileElem" multiple accept="image/*" onchange="handleFiles(this.files)">
</div>
<script>
var div = document.getElementById('drop-area');
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = div.offsetWidth;
canvas.height = div.offsetHeight;
ctx.font = "40px Arial";
ctx.fillStyle = "rgba(128, 128, 128, 0.5)";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Drag & Drop a File", div.offsetWidth / 2, div.offsetHeight / 2);
div.style.position = "relative";
div.appendChild(canvas);
canvas.style.position = "absolute";
canvas.style.top = 0;
canvas.style.left = 0;
canvas.style.zIndex = -1;
canvas.style.pointerEvents = "none";
let dropArea = document.getElementById("drop-area")
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false)
document.body.addEventListener(eventName, preventDefaults, false)
})
;['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false)
})
;['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false)
})
dropArea.addEventListener('drop', handleDrop, false)
function preventDefaults(e) {
e.preventDefault()
}
function highlight(e) {
dropArea.classList.add('highlight')
}
function unhighlight(e) {
dropArea.classList.remove('active')
}
function handleDrop(e) {
var dt = e.dataTransfer
var files = dt.files
handleFiles(files)
}
function handleFiles(files) {
console.info(files)
files = [...files]
files.forEach(uploadFile)
}
function uploadFile(file, i) {
let size = file.size
console.info('upload file', window.location.href)
var url = window.location.href
var xhr = new XMLHttpRequest()
var formData = new FormData()
xhr.open('POST', url, true)
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
let p = document.getElementById('progress')
p.style.display = "inline-block"
xhr.upload.onprogress = function (ev) {
if (ev.lengthComputable) {
p.max = ev.total
p.value = ev.loaded
}
}
xhr.addEventListener('readystatechange', function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
window.location.reload()
}
else if (xhr.readyState == 4 && xhr.status != 200) {
}
})
formData.append('file', file)
xhr.send(formData)
}
</script>
</body>
</html>