JavaScript - Simple Bytes Conversion

In this tutorial we will create a Simple Bytes Conversion using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. 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 href="https://sourcecodester.com" class="navbar-brand">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 Bytes Conversion</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <div class="form-inline">
  19.                                 <h4>Convert Bytes:</h4>
  20.                                 <input type="file" id="file" class="form-control" style="width:70%;"/>
  21.                                 <button onclick="getFileSize()" class="btn btn-primary"><span class="glyphicon glyphicon-random"></span> Convert</button>
  22.                                 <div id="result">               </div>
  23.                         </div>
  24.                 </div>
  25.         </div>
  26. <script src="js/script.js"></script>
  27. </body>
  28. </html>

Creating the Script

This code contains the script of the application. This code will convert the file size from bytes to readable 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. function getFileSize(){
  2.         var file = document.getElementById('file').files[0];
  3.        
  4.         if(file){
  5.                 document.getElementById('result').innerHTML = '<h4>The size of the uploaded file has <label style="color:blue;">'+convertBytes(file.size)+'</label></h4>';
  6.         }
  7. }
  8.  
  9. function convertBytes(num) {
  10.         var diff = num < 0;
  11.  
  12.         var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  13.  
  14.         if (diff){
  15.                 num = -num;
  16.         }
  17.  
  18.         if (num < 1){
  19.                 return (diff ? '-' : '') + num + ' B';
  20.         }
  21.        
  22.         var exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1);
  23.        
  24.         num = Number((num / Math.pow(1000, exponent)).toFixed(2));
  25.        
  26.         var unit = units[exponent];
  27.  
  28.         return (diff ? '-' : '') + num + ' ' + unit;
  29. }
There you have it we successfully created a Simple Bytes Conversion 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