-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy paththumbnail.js
67 lines (48 loc) · 1.98 KB
/
thumbnail.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
const myModal = new bootstrap.Modal(document.getElementById('thumbnailModal'), {keyboard: false});
const thumbBox = document.getElementById("thumbBox");
const uploadThumbBtn = document.getElementById("thumbnail");
const thumbDel = document.getElementById("thumbDelBtn");
const previewThumb = document.getElementById("thumbnailPreView");
const thumbUrl = document.getElementById("thumbnailUrl");
const thumbUrlUploadBtn = document.getElementById("thumbnail-url-upload-btn");
function uploadImg(input) {
if(input.files && input.files[0]) {
let token = getCsrfToken();
let formData = new FormData();
formData.append('img', input.files[0]);
const xhr = new XMLHttpRequest();
xhr.open("POST", "/article/uploadImg", false);
xhr.setRequestHeader("contentType", "multipart/form-data");
xhr.setRequestHeader("X-XSRF-TOKEN", token);
xhr.send(formData);
if (xhr.readyState === 4 && xhr.status === 200) {
thumbUrl.value = xhr.response;
previewThumb.src = thumbUrl.value;
// 썸네일 등록은 서버에서 하도록 리팩토링할것
// const reader = new FileReader();
// reader.onload = e => {
// previewThumb.src = e.target.result;
// }
// reader.readAsDataURL(input.files[0])
thumbBox.style.display = ''
myModal.hide();
} else {
alert("이미지가 정상적으로 업로드되지 못했습니다.")
}
}
}
thumbUrlUploadBtn.addEventListener("click", () =>{
const thumbUrlUploadInput = document.getElementById("thumbnail-url-upload-input");
const url = thumbUrlUploadInput.value;
previewThumb.src = url;
thumbUrl.value = url;
thumbBox.style.display = ''
myModal.hide();
})
uploadThumbBtn.addEventListener("change", e => {
uploadImg(e.target);
})
thumbDel.addEventListener("click", () =>{
thumbBox.style.display = 'none';
thumbUrl.value = "";
})