|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- Coding By CodingNepal - youtube.com/codingnepal --> |
| 3 | +<html lang="en"> |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <title>File Upload with Progress Bar | CodingNepal</title> |
| 8 | + |
| 9 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/> |
| 10 | +<style> |
| 11 | + @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap'); |
| 12 | + ::selection{ |
| 13 | + color: #fff; |
| 14 | + background: #6990F2; |
| 15 | + } |
| 16 | + .wrapper{ |
| 17 | + width: 430px; |
| 18 | + background: #fff; |
| 19 | + border-radius: 5px; |
| 20 | + padding: 30px; |
| 21 | + box-shadow: 7px 7px 12px rgba(0,0,0,0.05); |
| 22 | + } |
| 23 | + .wrapper header{ |
| 24 | + color: #6990F2; |
| 25 | + font-size: 27px; |
| 26 | + font-weight: 600; |
| 27 | + text-align: center; |
| 28 | + } |
| 29 | + .wrapper form{ |
| 30 | + height: 167px; |
| 31 | + display: flex; |
| 32 | + cursor: pointer; |
| 33 | + margin: 30px 0; |
| 34 | + align-items: center; |
| 35 | + justify-content: center; |
| 36 | + flex-direction: column; |
| 37 | + border-radius: 5px; |
| 38 | + border: 2px dashed #6990F2; |
| 39 | + } |
| 40 | + form :where(i, p){ |
| 41 | + color: #6990F2; |
| 42 | + } |
| 43 | + form i{ |
| 44 | + font-size: 50px; |
| 45 | + } |
| 46 | + form p{ |
| 47 | + margin-top: 15px; |
| 48 | + font-size: 16px; |
| 49 | + } |
| 50 | + |
| 51 | + section .row{ |
| 52 | + margin-bottom: 10px; |
| 53 | + background: #E9F0FF; |
| 54 | + list-style: none; |
| 55 | + padding: 15px 20px; |
| 56 | + border-radius: 5px; |
| 57 | + display: flex; |
| 58 | + align-items: center; |
| 59 | + justify-content: space-between; |
| 60 | + } |
| 61 | + section .row i{ |
| 62 | + color: #6990F2; |
| 63 | + font-size: 30px; |
| 64 | + } |
| 65 | + section .details span{ |
| 66 | + font-size: 14px; |
| 67 | + } |
| 68 | + .progress-area .row .content{ |
| 69 | + width: 100%; |
| 70 | + margin-left: 15px; |
| 71 | + } |
| 72 | + .progress-area .details{ |
| 73 | + display: flex; |
| 74 | + align-items: center; |
| 75 | + margin-bottom: 7px; |
| 76 | + justify-content: space-between; |
| 77 | + } |
| 78 | + .progress-area .content .progress-bar{ |
| 79 | + height: 6px; |
| 80 | + width: 100%; |
| 81 | + margin-bottom: 4px; |
| 82 | + background: #fff; |
| 83 | + border-radius: 30px; |
| 84 | + } |
| 85 | + .content .progress-bar .progress{ |
| 86 | + height: 100%; |
| 87 | + width: 0%; |
| 88 | + background: #6990F2; |
| 89 | + border-radius: inherit; |
| 90 | + } |
| 91 | + .uploaded-area{ |
| 92 | + max-height: 232px; |
| 93 | + overflow-y: scroll; |
| 94 | + } |
| 95 | + .uploaded-area.onprogress{ |
| 96 | + max-height: 150px; |
| 97 | + } |
| 98 | + .uploaded-area::-webkit-scrollbar{ |
| 99 | + width: 0px; |
| 100 | + } |
| 101 | + .uploaded-area .row .content{ |
| 102 | + display: flex; |
| 103 | + align-items: center; |
| 104 | + } |
| 105 | + .uploaded-area .row .details{ |
| 106 | + display: flex; |
| 107 | + margin-left: 15px; |
| 108 | + flex-direction: column; |
| 109 | + } |
| 110 | + .uploaded-area .row .details .size{ |
| 111 | + color: #404040; |
| 112 | + font-size: 11px; |
| 113 | + } |
| 114 | + .uploaded-area i.fa-check{ |
| 115 | + font-size: 16px; |
| 116 | + } |
| 117 | + </style> |
| 118 | + |
| 119 | +</head> |
| 120 | +<body> |
| 121 | + <div class="wrapper"> |
| 122 | + <header>File Uploader JavaScript</header> |
| 123 | + <form action="#"> |
| 124 | + <input class="file-input" type="file" name="file" hidden> |
| 125 | + <i class="fas fa-cloud-upload-alt"></i> |
| 126 | + <p>Browse File to Upload</p> |
| 127 | + </form> |
| 128 | + <section class="progress-area"></section> |
| 129 | + <section class="uploaded-area"></section> |
| 130 | + </div> |
| 131 | + |
| 132 | + <script>const form = document.querySelector("form"), |
| 133 | + fileInput = document.querySelector(".file-input"), |
| 134 | + progressArea = document.querySelector(".progress-area"), |
| 135 | + uploadedArea = document.querySelector(".uploaded-area"); |
| 136 | + |
| 137 | + // form click event |
| 138 | + form.addEventListener("click", () =>{ |
| 139 | + fileInput.click(); |
| 140 | + }); |
| 141 | + |
| 142 | + fileInput.onchange = ({target})=>{ |
| 143 | + let file = target.files[0]; //getting file [0] this means if user has selected multiple files then get first one only |
| 144 | + if(file){ |
| 145 | + let fileName = file.name; //getting file name |
| 146 | + if(fileName.length >= 12){ //if file name length is greater than 12 then split it and add ... |
| 147 | + let splitName = fileName.split('.'); |
| 148 | + fileName = splitName[0].substring(0, 13) + "... ." + splitName[1]; |
| 149 | + } |
| 150 | + uploadFile(fileName); //calling uploadFile with passing file name as an argument |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // file upload function |
| 155 | + function uploadFile(name){ |
| 156 | + let xhr = new XMLHttpRequest(); //creating new xhr object (AJAX) |
| 157 | + xhr.open("POST", "php/upload.php"); //sending post request to the specified URL |
| 158 | + xhr.upload.addEventListener("progress", ({loaded, total}) =>{ //file uploading progress event |
| 159 | + let fileLoaded = Math.floor((loaded / total) * 100); //getting percentage of loaded file size |
| 160 | + let fileTotal = Math.floor(total / 1000); //gettting total file size in KB from bytes |
| 161 | + let fileSize; |
| 162 | + // if file size is less than 1024 then add only KB else convert this KB into MB |
| 163 | + (fileTotal < 1024) ? fileSize = fileTotal + " KB" : fileSize = (loaded / (1024*1024)).toFixed(2) + " MB"; |
| 164 | + let progressHTML = `<li class="row"> |
| 165 | + <i class="fas fa-file-alt"></i> |
| 166 | + <div class="content"> |
| 167 | + <div class="details"> |
| 168 | + <span class="name">${name} • Uploading</span> |
| 169 | + <span class="percent">${fileLoaded}%</span> |
| 170 | + </div> |
| 171 | + <div class="progress-bar"> |
| 172 | + <div class="progress" style="width: ${fileLoaded}%"></div> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </li>`; |
| 176 | + // uploadedArea.innerHTML = ""; //uncomment this line if you don't want to show upload history |
| 177 | + uploadedArea.classList.add("onprogress"); |
| 178 | + progressArea.innerHTML = progressHTML; |
| 179 | + if(loaded == total){ |
| 180 | + progressArea.innerHTML = ""; |
| 181 | + let uploadedHTML = `<li class="row"> |
| 182 | + <div class="content upload"> |
| 183 | + <i class="fas fa-file-alt"></i> |
| 184 | + <div class="details"> |
| 185 | + <span class="name">${name} • Uploaded</span> |
| 186 | + <span class="size">${fileSize}</span> |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + <i class="fas fa-check"></i> |
| 190 | + </li>`; |
| 191 | + uploadedArea.classList.remove("onprogress"); |
| 192 | + // uploadedArea.innerHTML = uploadedHTML; //uncomment this line if you don't want to show upload history |
| 193 | + uploadedArea.insertAdjacentHTML("afterbegin", uploadedHTML); //remove this line if you don't want to show upload history |
| 194 | + } |
| 195 | + }); |
| 196 | + let data = new FormData(form); //FormData is an object to easily send form data |
| 197 | + xhr.send(data); //sending form data |
| 198 | + } |
| 199 | + </script> |
| 200 | + |
| 201 | +</body> |
| 202 | +</html> |
0 commit comments