JavaScript - Simple File Size Calculator

In this tutorial we will create a Simple File Size Calculator using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple File Size Calculator</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <div class="form-inline">
  19.                                 <input type="number" class="form-control" onkeyup="calculateSize(this.value);"/>
  20.                                 <select class="form-control" id="file_type">
  21.                                         <option value="Byte">Byte</option>
  22.                                         <option value="KB">KB</option>
  23.                                         <option value="MB">MB</option>
  24.                                         <option value="GB">GB</option>
  25.                                 </select>
  26.                         </div>
  27.                         <br />
  28.                         <div id="result" style="text-align:center; font-size:26px;"></div>
  29.                 </div>
  30.         </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will automatically convert to any file size. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var total_size = 0;
  2.  
  3. function calculateSize(val){
  4.         if(val != ""){
  5.                 var type = document.getElementById('file_type').value;
  6.                 switch(type){
  7.                         case 'Byte':
  8.                                 total_size = val;
  9.                         break;
  10.                         case 'KB':
  11.                                 total_size = val * 1024;
  12.                         break;
  13.                         case 'MB':
  14.                                 total_size = val * 1024 * 1024;
  15.                         break;
  16.                         case 'GB':
  17.                                 total_size = val * 1024 * 1024 * 1024;
  18.                         break;
  19.                 }
  20.                
  21.                 document.getElementById('result').innerHTML = "<label class='text-info'>"+total_size+"</label> Bytes<br /><label class='text-info'>"+Math.round(total_size/1024)+"</label> KB<br /><label class='text-info'>"+Math.round(total_size/1024/1024)+"</label> MB<br /><label class='text-info'>"+Math.round(total_size/1024/1024/1024)+"</label> GB";
  22.         }else{
  23.                 document.getElementById('result').innerHTML = "";
  24.         }
  25. }
There you have it we successfully created a Simple File Size Calculator using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment