How to Dynamically Convert File Size in JavaScript

How to Dynamically Convert File Size in JavaScript


In this tutorial we will create a How to Dynamically Convert File Size in JavaScript. This tutorial purpose is to teach you on how to convert any file size. This will tackle all the basic function that will convert the file size you have entered. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you to know the conversion value of any file size. I will give my best to provide you the easiest way of creating this program Convert File Size. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display html form and buttons. To create this simply copy and write it into your text editor, then save it 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="">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">How to Dynamically Convert File Size</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-5">
  17. <div class="form-group">
  18. <label>Enter here</label>
  19. <input type="number" class="form-control" id="number1"/>
  20. </div>
  21. <label>From</label>
  22. <div class="form-inline">
  23. <select class="form-control" id="file1" style="width:50%;">
  24. <option value="Byte">Byte</option>
  25. <option value="KB">KB</option>
  26. <option value="MB">MB</option>
  27. <option value="GB">GB</option>
  28. </select>
  29. <br />
  30. <label>To</label>
  31. <br />
  32. <select class="form-control" id="file2" style="width:50%;">
  33. <option value="Byte">Byte</option>
  34. <option value="KB">KB</option>
  35. <option value="MB">MB</option>
  36. <option value="GB">GB</option>
  37. </select>
  38. </div>
  39. <br />
  40. <div class="form-group">
  41. <h2>RESULT</h2>
  42. <input type="text" class="form-control" id="number2" disabled="disabled"/>
  44. </div>
  45. <button class="btn btn-primary" onclick="convertFileSize();">Convert</button>
  46. </div>
  48. </div>
  49. </div>
  50. <script src="script.js"></script>
  51. </body>
  52. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically convert your the file size when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function convertFileSize(){
  2. let number=document.getElementById('number1').value;
  4. if(number==""){
  5. alert("Please enter something");
  6. }else{
  7. let result;
  8. let file1=document.getElementById('file1').value;
  9. let file2=document.getElementById('file2').value;
  11. if(file1=="Byte" && file2=="Byte"){
  12. result=parseInt(number);
  13. }else if(file1=="Byte" && file2=="KB"){
  14. result=Math.round(parseInt(number)/1024)
  15. }else if(file1=="Byte" && file2=="MB"){
  16. result=Math.round(parseInt(number)/1024/1024);
  17. }else if(file1=="Byte" && file2=="GB"){
  18. result=Math.round(parseInt(number)/1024/1024/1024);
  19. }else if(file1=="KB" && file2=="Byte"){
  20. result=parseInt(number)*1024;
  21. }else if(file1=="KB" && file2=="KB"){
  22. result=parseInt(number);
  23. }else if(file1=="KB" && file2=="MB"){
  24. result=Math.round(parseInt(number)/1024);
  25. }else if(file1=="KB" && file2=="GB"){
  26. result=Math.round(parseInt(number)/1024/1024);
  27. }else if(file1=="MB" && file2=="Byte"){
  28. result=parseInt(number)*1024*1024;
  29. }else if(file1=="MB" && file2=="KB"){
  30. result=parseInt(number)*1024;
  31. }else if(file1=="MB" && file2=="MB"){
  32. result=parseInt(number);
  33. }else if(file1=="MB" && file2=="GB"){
  34. result=Math.round(parseInt(number)/1024);
  35. }else if(file1=="GB" && file2=="Byte"){
  36. result=parseInt(number)*1024*1024*1024;
  37. }else if(file1=="GB" && file2=="KB"){
  38. result=parseInt(number)*1024*1024;
  39. }else if(file1=="GB" && file2=="MB"){
  40. result=parseInt(number)*1024;
  41. }else if(file1=="GB" && file2=="GB"){
  42. result=parseInt(number);
  43. }
  45. document.getElementById('number2').value = result;
  46. }
  47. }

In the code above we will only create a method called convertFileSize(), this function will convert the file size you have entered to a specific value. This function contains several conditional statement that handle the conversion for the specific file size.


The How to Dynamically Convert File Size in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Convert File Size in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment