This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_files.html
222 lines (182 loc) · 5.04 KB
/
upload_files.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<!-- https://wicg.github.io/directory-upload/index.html -->
<html>
<head>
<title>Directory Upload Example</title>
<style type="text/css">
* {
box-sizing: border-box;
}
body {
font-family: Arial;
padding: 40px;
margin: 0;
}
h1 {
margin-top: 0;
}
h1, h2 {
color: #0576A1;
}
a {
text-decoration: none;
color: #0074D9;
}
a:hover {
color: #42A1F5;
}
#dropDiv {
width: 100%;
border: 1px solid #CCCCCC;
margin-top: 20px;
padding: 80px 20px;
text-align: center;
color: #CCCCCC;
}
#dropDiv.over {
border-color: #DE7E7E;
color: #DE7E7E;
background-color: #FFEDED;
}
pre.code {
background-color: #F9F9F9;
padding: 10px;
display: block;
display: none;
border: 1px solid #EBEBEB;
}
#console {
border: 1px solid #EBEBEB;
background-color: #F9F9F9;
color: #333333;
padding: 10px;
}
</style>
</head>
<body>
<h1>Actyx Files Upload Demo</h1>
<p>After obtaining and pasting an authorization token, you can use
this page to upload files and folders to the locally attached Actyx
node.</p>
<h4><a href="#" onclick="return toggleDisplay('code-fileinput');">How to obtain an authorization token</a></h4>
<pre class="code" id="code-fileinput">
curl -s localhost:4454/api/v2/auth \ <<<
-d'{"appId":"com.example.app","displayName": "Example App","version": "1.0"}' \
-H "Content-Type: application/json" \
| jq -r '.token'
</pre>
<label for="token">Authorization token:<label>
<input type="text" id="token">
<div id="dropDiv">Drag & drop your files here!</div>
<div>
<h2>Upload</h2>
<p>Clear the file list by reloading the page.</p>
<pre id="chosenFiles"></pre>
<button id="uploadButton" type="button" onclick="return upload();">Upload</button>
<h3>4. Log</h3>
<pre id="console"></pre>
<script type="text/javascript">
let files = [];
function toggleDisplay(id) {
var el = document.getElementById(id);
el.style.display = el.style.display === 'block' ? 'none' : 'block';
return false;
}
function upload() {
uploadButton.disabled = true;
const oldLabel = uploadButton.innerHTML;
uploadButton.innerHTML = "Processing ..";
console.log("Uploading", files);
let formData = new FormData();
for (const { fileName, blob } of files) {
formData.append('file', blob, fileName);
}
fetch("http://localhost:4454/api/v2/files", {
method: 'POST',
mode: 'cors',
headers: {
Authorization: "Bearer " + token.value,
},
body: formData
})
.then(async (resp) => {
uploadButton.disabled = false;
uploadButton.innerHTML = oldLabel;
const text = await resp.text();
if (resp.status !== 200) {
throw new Error(text);
} else {
printToScreen("Success: " + JSON.stringify(text));
}
})
.catch(err => {
uploadButton.disabled = false;
uploadButton.innerHTML = oldLabel;
printToScreen("Error: " + err.message);
})
}
function printToScreen(str) {
var cons = document.getElementById('console');
cons.innerHTML += '<br>';
cons.innerHTML += str;
}
document.addEventListener('DOMContentLoaded', function(event) {
function clearCons() {
var cons = document.getElementById('console');
cons.innerHTML = ''
}
clearCons();
function addFiles(item, container) {
let elem = document.createElement("li");
elem.textContent = item.name;
container.appendChild(elem);
if (item.isDirectory) {
let directoryReader = item.createReader();
let directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries) {
entries.forEach(function(entry) {
addFiles(entry, directoryContainer);
});
}, function(err) {
console.error(err);
printToScreen(err);
});
} else {
item.file(function(blob) {
files.push({ fileName: item.fullPath, blob });
});
}
}
const hoverClassName = "over";
// Handle drag* events to handle style
// Add the css you want when the class "hover" is present
dropDiv.addEventListener("dragenter", function (e) {
e.preventDefault();
dropDiv.classList.add(hoverClassName);
});
dropDiv.addEventListener("dragover", function (e) {
e.preventDefault();
dropDiv.classList.add(hoverClassName);
});
dropDiv.addEventListener("dragleave", function (e) {
e.preventDefault();
dropDiv.classList.remove(hoverClassName);
});
// This is the most important event, the event that gives access to files
dropDiv.addEventListener("drop", function (e) {
e.preventDefault();
dropDiv.classList.remove(hoverClassName);
const items = e.dataTransfer.items;
for (let i=0; i<items.length; i++) {
let item = items[i].webkitGetAsEntry();
if (item) {
console.log(item)
addFiles(item, chosenFiles)
}
}
});
});
</script>
</body>
</html>