Skip to main content

JavaScript - File Size Converter


In this tutorial we will create a File Size Converter using JavaScript. This code will calculate the given file size when the user click the button. The code use onclick() to initiate the calculateSize() function that compute the actual size of the given value by multiplying it or by dividing it by 1024 . This is a free program, you can modify it and use it as your own.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, 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 - File Size Converter</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" id="number1"/>
  20. <select class="form-control" id="file1">
  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 class="form-inline">
  29. <input type="number" class="form-control" id="number2" disabled="disabled"/>
  30. <select class="form-control" id="file2">
  31. <option value="Byte">Byte</option>
  32. <option value="KB">KB</option>
  33. <option value="MB">MB</option>
  34. <option value="GB">GB</option>
  35. </select>
  36. </div>
  37. <br />
  38. <center><button class="btn btn-primary" onclick="calculateSize();">Calculate</button></center>
  39. </div>
  40. </div>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will compute the file size when button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function calculateSize(){
  2. var number=document.getElementById('number1').value;
  3.  
  4. if(number==""){
  5. alert("Please enter something");
  6. }else{
  7. var result;
  8. var file1=document.getElementById('file1').value;
  9. var file2=document.getElementById('file2').value;
  10.  
  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. }
  44.  
  45. document.getElementById('number2').value = result;
  46. }
  47. }

There you have it we successfully created a File Size Converter 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.