250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- xcode
- effect
- CSS
- iOS 개발자
- MAC
- 자바스크립트
- 개발자
- css3
- 풀스택
- button
- hover
- html5
- jQuery
- SWIFT
- keyframes
- HTML
- IOS
- 비전공 개발자
- javascript
- image
- 백엔드
- react
- 프론트엔드
- Animation
- iPhone
- ipad
- php
- front-end
- 애니메이션
- 비전공자
Archives
- Today
- Total
비전공자 개발일기
Drag & Drop File Upload 본문
728x90
SMALL
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Drag, Drop & Browse</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="style.css" />
<script defer src="main.js"></script>
</head>
<body>
<div class="container">
<h3>Upload your File :</h3>
<div class="drag-area">
<div class="icon">
<i class="fas fa-images"></i>
</div>
<span class="header">Drag & Drop</span>
<span class="header">or <span class="button">browse</span></span>
<input type="file" hidden />
<span class="support">Supports: JPEG, JPG, PNG</span>
</div>
<div>
<button class="download">Download</button>
</div>
</div>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 100vh;
background: #e0eafc; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #cfdef3, #e0eafc); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(
to right,
#cfdef3,
#e0eafc
); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
.container {
max-width: 650px;
width: 100%;
padding: 30px;
background: #fff;
border-radius: 20px;
box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
}
.drag-area {
height: 400px;
border: 3px dashed #e0eafc;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 10px auto;
}
h3 {
margin-bottom: 20px;
font-weight: 500;
}
.drag-area .icon {
font-size: 50px;
color: #1683ff;
}
.drag-area .header {
font-size: 20px;
font-weight: 500;
color: #34495e;
}
.drag-area .support {
font-size: 12px;
color: gray;
margin: 10px 0 15px 0;
}
.drag-area .button {
font-size: 20px;
font-weight: 500;
color: #1683ff;
cursor: pointer;
}
.drag-area.active {
border: 2px solid #1683ff;
}
.drag-area img {
width: 100%;
height: 100%;
object-fit: cover;
}
.download {
background-color:#4CAF50;
border: none;
color: white;
padding: 6px 32px;
text-align: center;
const dropArea = document.querySelector(".drag-area");
const dragText = document.querySelector(".header");
let button = dropArea.querySelector(".button");
let input = dropArea.querySelector("input");
let file;
button.onclick = () => {
input.click();
};
// when browse
input.addEventListener("change", function () {
file = this.files[0];
dropArea.classList.add("active");
displayFile();
});
// when file is inside drag area
dropArea.addEventListener("dragover", (event) => {
event.preventDefault();
dropArea.classList.add("active");
dragText.textContent = "Release to Upload";
// console.log('File is inside the drag area');
});
// when file leave the drag area
dropArea.addEventListener("dragleave", () => {
dropArea.classList.remove("active");
// console.log('File left the drag area');
dragText.textContent = "Drag & Drop";
});
// when file is dropped
dropArea.addEventListener("drop", (event) => {
event.preventDefault();
// console.log('File is dropped in drag area');
file = event.dataTransfer.files[0]; // grab single file even of user selects multiple files
// console.log(file);
displayFile();
});
function displayFile() {
let fileType = file.type;
// console.log(fileType);
let validExtensions = ["image/jpeg", "image/jpg", "image/png"];
if (validExtensions.includes(fileType)) {
// console.log('This is an image file');
let fileReader = new FileReader();
fileReader.onload = () => {
let fileURL = fileReader.result;
// console.log(fileURL);
let imgTag = `<img src="${fileURL}" alt="">`;
dropArea.innerHTML = imgTag;
};
fileReader.readAsDataURL(file);
} else {
alert("This is not an Image File");
dropArea.classList.remove("active");
}
}
728x90
LIST
'Javascript' 카테고리의 다른 글
Right Click Menu (0) | 2022.11.12 |
---|---|
Spin Wheel (0) | 2022.11.09 |
Image Editor (0) | 2022.11.03 |
Captcha validation (0) | 2022.10.31 |
Parallax Tilt Effect Cards (0) | 2022.10.29 |