-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadform.go
80 lines (64 loc) · 1.96 KB
/
uploadform.go
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
package main
import (
"strconv"
)
func (s *S3pal) getUploadForm() string {
uploadEndpoint := "http://" + s.Config.Server.Host + ":" + strconv.Itoa(s.Config.Server.Port) + "/upload/file"
return `<html>
<title>s3pal uploader to ` + s.Config.Aws.Bucket + `</title>
<style type="text/css">
.box {
float:left;width:450px;height:450px;float:left;border:1px solid black;margin:5px;padding:5px;
}
</style>
<body>
<h1>s3pal</h1>
<p>This needs to look better.</p>
<div class="box">
<h2>Upload to ` + s.Config.Aws.Bucket + `</h2>
<form action="` + uploadEndpoint + `/upload/file" method="post" enctype="multipart/form-data" id="upload-form">
Prefix: <input type="text" name="prefix" value="` + s.Config.Server.Prefix + `" style="width:200px">
<br>
<p id="msg">Drag/Drop file</p>
<input type="file" name="file" id="file" style="width:200px;height:200px;border:1px dashed #ccc;;">
<br><br>
<input type="button" id="upload" value="Upload">
<br>
</form>
</div>
<div class="box">
<h2>Result</h2>
<div id="result"></div>
</div>
<script>
var uploadForm = document.getElementById("upload-form");
var doUpload = function() {
uploadForm.style.display = 'none';
document.getElementById("msg").innerHTML = 'Uploading...';
var uploadData = new FormData(uploadForm);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4) {
document.getElementById("msg").innerHTML = 'Done.';
var json = JSON.parse(xhr.responseText);
var a = document.createElement("a");
a.href = json.url;
a.textContent = json.url;
document.getElementById('result').appendChild(a);
}
}
xhr.open("POST", "` + uploadEndpoint + `", true);
console.log(uploadData);
xhr.send(uploadData);
}
uploadForm.addEventListener("change", function(e) {
doUpload();
});
document.getElementById('upload').addEventListener("click", function(e) {
doUpload();
});
</script>
</body>
</html>
`
}