• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - File Size Converter

sersha

Codebase Quality Auditor
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
165
Likes
114
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    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

    ;

  3. if

    (

    number==

    ""

    )

    {
  4. alert(

    "Please enter something"

    )

    ;
  5. }

    else

    {
  6. var

    result;
  7. var

    file1=

    document.getElementById

    (

    'file1'

    )

    .value

    ;
  8. var

    file2=

    document.getElementById

    (

    'file2'

    )

    .value

    ;

  9. if

    (

    file1==

    "Byte"

    &&

    file2==

    "Byte"

    )

    {
  10. result=

    parseInt(

    number)

    ;
  11. }

    else

    if

    (

    file1==

    "Byte"

    &&

    file2==

    "KB"

    )

    {
  12. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    )
  13. }

    else

    if

    (

    file1==

    "Byte"

    &&

    file2==

    "MB"

    )

    {
  14. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    /

    1024

    )

    ;
  15. }

    else

    if

    (

    file1==

    "Byte"

    &&

    file2==

    "GB"

    )

    {
  16. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    /

    1024

    /

    1024

    )

    ;
  17. }

    else

    if

    (

    file1==

    "KB"

    &&

    file2==

    "Byte"

    )

    {
  18. result=

    parseInt(

    number)

    *

    1024

    ;
  19. }

    else

    if

    (

    file1==

    "KB"

    &&

    file2==

    "KB"

    )

    {
  20. result=

    parseInt(

    number)

    ;
  21. }

    else

    if

    (

    file1==

    "KB"

    &&

    file2==

    "MB"

    )

    {
  22. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    )

    ;
  23. }

    else

    if

    (

    file1==

    "KB"

    &&

    file2==

    "GB"

    )

    {
  24. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    /

    1024

    )

    ;
  25. }

    else

    if

    (

    file1==

    "MB"

    &&

    file2==

    "Byte"

    )

    {
  26. result=

    parseInt(

    number)

    *

    1024

    *

    1024

    ;
  27. }

    else

    if

    (

    file1==

    "MB"

    &&

    file2==

    "KB"

    )

    {
  28. result=

    parseInt(

    number)

    *

    1024

    ;
  29. }

    else

    if

    (

    file1==

    "MB"

    &&

    file2==

    "MB"

    )

    {
  30. result=

    parseInt(

    number)

    ;
  31. }

    else

    if

    (

    file1==

    "MB"

    &&

    file2==

    "GB"

    )

    {
  32. result=

    Math

    .round

    (

    parseInt(

    number)

    /

    1024

    )

    ;
  33. }

    else

    if

    (

    file1==

    "GB"

    &&

    file2==

    "Byte"

    )

    {
  34. result=

    parseInt(

    number)

    *

    1024

    *

    1024

    *

    1024

    ;
  35. }

    else

    if

    (

    file1==

    "GB"

    &&

    file2==

    "KB"

    )

    {
  36. result=

    parseInt(

    number)

    *

    1024

    *

    1024

    ;
  37. }

    else

    if

    (

    file1==

    "GB"

    &&

    file2==

    "MB"

    )

    {
  38. result=

    parseInt(

    number)

    *

    1024

    ;
  39. }

    else

    if

    (

    file1==

    "GB"

    &&

    file2==

    "GB"

    )

    {
  40. result=

    parseInt(

    number)

    ;
  41. }

  42. document.getElementById

    (

    'number2'

    )

    .value

    =

    result;
  43. }
  44. }

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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top