-
Notifications
You must be signed in to change notification settings - Fork 0
/
reuse.txt
84 lines (66 loc) · 2.72 KB
/
reuse.txt
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
function uploadFile(fileObject, file_id) {
const endpoint = '/api/upload-file/' + file_id;
const bearer = 'Bearer ' + loginData.token;
fetch(endpoint, {
method: 'POST',
headers: {
'Authorization': bearer
},
body: fileObject
}).then(res => {
if (res.status === 201) {
console.log(res.json());
const response = Response.parse(this);
//var rezultat = JSON.parse(res);
//update_progressBar(rezultat.size);
var ul = document.getElementById('dynamic_files');
var listItem = document.createElement("li");
listItem.setAttribute("id", file_id);
var downbtn = document.createElement("button");
var delbtn = document.createElement("button");
downbtn.className = "download-btn";
downbtn.textContent = "DOWNLOAD";
delbtn.className = "delete-btn";
delbtn.textContent = "DELETE";
downbtn.onclick = function() {
console.log(this.parentElement);
const url = '/api/download-file/' + this.parentElement.id;
const authHeader = 'Bearer ' + loginData.token;
const options = {
headers: {
Authorization: authHeader
}
};
fetch(url, options).then(res => res.blob()).then(blob => {
var file = window.URL.createObjectURL(blob);
var filename = this.parentElement.getElementsByClassName('dynamic-btn')[0].innerText;
var a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
a.href = file;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
})
};
delbtn.onclick = function() {
console.log(this.parentElement);
const url = '/api/delete-file/' + this.parentElement.id;
const authHeader = 'Bearer ' + loginData.token;
const options = {
method: 'DELETE',
headers: {
Authorization: authHeader
}
};
fetch(url, options);
this.parentElement.remove();
};
listItem.innerHTML = "<button class=\"dynamic-btn\">" + fileObject.name + "</button>";
ul.appendChild(listItem);
listItem.appendChild(downbtn);
listItem.appendChild(delbtn);
}
});
}