blob: 1f2585b78fbae27f01b3c8e6f5bfadc64dc2d7eb (
plain)
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
|
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Box</title>
<script>
function upload() {
let resourceId = "";
token = document.getElementById("token");
if (token == null) {
token = "";
} else {
token = token.value;
}
f = document.getElementById("file").files[0];
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", (e) => {
p = document.getElementById("progress")
ratio = e.loaded / e.total;
perc = Math.floor(ratio * 100);
p.innerHTML = perc + "%";
});
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status != 200) {
if (xhr.status != 0) {
document.getElementById("progress").innerHTML = "Error: " + xhr.status + " " + xhr.statusText;
} else {
document.getElementById("progress").innerHTML = "Error: unknown";
}
} else {
document.getElementById("progress").innerHTML = "Done: <a href=\"/" + resourceId + "\">" + resourceId + "</a>";
}
}
if (xhr.readyState === xhr.HEADERS_RECEIVED) {
resourceId = xhr.getResponseHeader("X-Resource-ID");
}
}
xhr.open("POST", "/upload", true);
xhr.setRequestHeader("X-Token", token)
xhr.send(f)
}
</script>
</head>
<body>
<pre>Server for uploading files.
Use the form here or send a POST request to /upload with the content of the file in the body.
If a token has been set on the server, pass the token in the request via a X-Token header.
The response will contain a X-Resource-ID header containing the ID of the saved file.
This ID can then be used to get the file by sending a GET request to /[resourceID].
If enabled on the server, the resource can be deleted by sending a DELETE request to /[resourceID].
Again if a token has been set on the server, use a X-Token header when sending the request.</pre>
{{ if ne . "" }}
<input type="text" id="token" placeholder="token"/>
{{end}}
<input type="file" id="file"/><br/><br/>
<button type="button" onclick="upload()">Upload</button>
<span id="progress"></span>
</body>
</html>
|