다음은 업로드하는 동안 JavaScript에서 파일 크기를 확인하는 코드입니다 -
예시
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>Validating a file size while uploading</h1> <input type="file" class="file" onchange="validateFile()" /> <div class="result"></div> <h3>Upload a file using the above file input type</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let fileEle = document.querySelector(".file"); function validateFile() { if (fileEle.files.length) { let fileSize = Math.round(fileEle.files[0].size / 1024); if (fileSize > 4096) { resEle.innerHTML = "File cant be uploaded too big"; } else { resEle.innerHTML = "File can be uploaded. File size = " + fileSize+’ bytes’; } } } </script> </body> </html>
출력
'파일 선택' 버튼을 클릭하고 파일을 선택하면 -