JavaScript - File Size Converter

Submitted by: 
Visitors have accessed this post 128 times.

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

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - File Size Converter</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div class="form-inline">
  20. <input type="number" class="form-control" id="number1"/>
  21. <select class="form-control" id="file1">
  22. <option value="Byte">Byte</option>
  23. <option value="KB">KB</option>
  24. <option value="MB">MB</option>
  25. <option value="GB">GB</option>
  26. </select>
  27. </div>
  28. <br />
  29. <div class="form-inline">
  30. <input type="number" class="form-control" id="number2" disabled="disabled"/>
  31. <select class="form-control" id="file2">
  32. <option value="Byte">Byte</option>
  33. <option value="KB">KB</option>
  34. <option value="MB">MB</option>
  35. <option value="GB">GB</option>
  36. </select>
  37. </div>
  38. <br />
  39. <center><button class="btn btn-primary" onclick="calculateSize();">Calculate</button></center>
  40. </div>
  41. </div>
  42. <script src="js/script.js"></script>
  43. </body>
  44. </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;
  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;
  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. }

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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.