-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (95 loc) · 3.06 KB
/
script.js
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
// File upload
var inputFile = document.querySelector("input[type=file]"); {
inputFile.onchange = function (e) {
try {
e.stopPropagation();
e.preventDefault();
var files = e.target.files;
for (var i=0;i<files.length;i++) {
GetFile(files[i]);
}
return false;
}
catch (e) {
console.error(e);
}
};
}
// Drag and drop feature
var element = document.body; {
element.draggable = false;
element.ondragover = function (e) {
e.preventDefault();
return false;
};
element.ondragleave = element.ondragend = function (e) {
return false;
};
element.ondrop = function (e) {
try {
e.preventDefault();
var files = e.dataTransfer.files;
for (var i=0;i<files.length;i++) {
GetFile(files[i]);
}
return false;
}
catch (e) {
console.error(e);
}
}
}
function fileReaderLoad(e,inStorage) {
var wrap = document.createElement("div");
wrap.classList.add("filerow");
if (e.target.result.indexOf("image/")===5) {
// image
var img=wrap.appendChild(document.createElement("div"));
img.classList.add("img");
img.style.backgroundImage="url('"+e.target.result+"')";
}else if (e.target.result.indexOf("audio/")===5) {
// audio
var aud = wrap.appendChild(new Audio());
aud.setAttribute("controls","true");
aud.classList.add("col1");
aud.src = e.target.result;
}else{
var fill=wrap.appendChild(document.createElement("div"));
fill.classList.add("fill");
}
var info=wrap.appendChild(document.createElement("div"));
var link = info.appendChild(document.createElement("a"));
link.textContent="direct link";
link.href=e.target.result;
info.appendChild(document.createElement("br"));
document.querySelector(".preview").appendChild(wrap);
if (sessionStorage && !inStorage) {
sessionStorage["file_"+Date.now().toString(36)]=e.target.result;
}
}
function GetFile(f) {
var fileReader = new FileReader();
fileReader.onload = fileReaderLoad;
fileReader.readAsDataURL(f);
}
document.querySelector("#clearBtn").addEventListener("click",function () {
document.querySelector(".preview").innerHTML="";
if (sessionStorage) {
for (var i in sessionStorage) {
if (i.indexOf("file_")===0) {
sessionStorage.removeItem(i);
}
}
}
})
window.addEventListener("load",function () {
if (sessionStorage) {
for (var i in sessionStorage) {
if (i.indexOf("file_")===0) {
// Show the previous loaded files.
// Might not be the smartest way to do this.
fileReaderLoad({target:{result:sessionStorage[i]}},true);
}
}
}
});