-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuploadPage.js
More file actions
49 lines (41 loc) · 1.35 KB
/
uploadPage.js
File metadata and controls
49 lines (41 loc) · 1.35 KB
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
function getImageFiles(e) {
const uploadFiles = [];
const files = e.currentTarget.files;
const imagePreview = document.querySelector(".image-preview");
const docFrag = new DocumentFragment();
if ([...files].length >= 7) {
alert("이미지는 최대 6개 까지 업로드가 가능합니다.");
return;
}
// 파일 타입 검사
[...files].forEach((file) => {
if (!file.type.match("image/.*")) {
alert("이미지 파일만 업로드가 가능합니다.");
return;
}
// 파일 갯수 검사
if ([...files].length < 7) {
uploadFiles.push(file);
const reader = new FileReader();
reader.onload = (e) => {
const preview = createElement(e, file);
imagePreview.appendChild(preview);
};
reader.readAsDataURL(file);
}
});
}
// 업로드 이미지 파일 preview 표현
function createElement(e, file) {
const li = document.createElement("li");
const img = document.createElement("img");
img.setAttribute("src", e.target.result);
img.setAttribute("data-file", file.name);
li.appendChild(img);
return li;
}
// 클릭시 input 이벤트 발생
const realUpload = document.querySelector(".real-upload");
const upload = document.querySelector(".upload");
upload.addEventListener("click", () => realUpload.click());
realUpload.addEventListener("change", getImageFiles);