How to get Uploaded File Size Value in JavaScript

How to get Uploaded File Size Value in JavaScript

Introduction

In this tutorial we will create a How to get Uploaded File Size Value in JavaScript. This tutorial purpose is to enable you to display the file size of the uploaded file. This will cover all the basic function that will search a list. 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 that needed to display the specific file size. I will give my best to provide you the easiest way of creating this program Display Uploaded File Size Value. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface to our application. This code will only display upload form and button. 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 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">How to get Uploaded File Size Value</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <div class="form-inline">
  18. <h4>UPLOAD FILE HERE</h4>
  19. <input type="file" id="file" class="form-control" style="width:100%;"/>
  20. <br /><br />
  21. <button onclick="getFileSize()" class="btn btn-primary">Upload</button>
  22. <div id="result"></div>
  23. </div>
  24. </div>
  25. </div>
  26. <script src="script.js"></script>
  27. </body>
  28. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will get the file size of the uploaded file from the html input form. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function convertBytes(num) {
  2. let diff = num < 0;
  3.  
  4. let units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  5.  
  6. if (diff){
  7. num = -num;
  8. }
  9.  
  10. if (num < 1){
  11. return (diff ? '-' : '') + num + ' B';
  12. }
  13.  
  14. let exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1);
  15.  
  16. num = Number((num / Math.pow(1000, exponent)).toFixed(2));
  17.  
  18. let unit = units[exponent];
  19.  
  20. return (diff ? '-' : '') + num + ' ' + unit;
  21. }
  22.  
  23. function getFileSize(){
  24. let file = document.getElementById('file').files[0];
  25. let fileName = file.name;
  26. if(file){
  27. document.getElementById('result').innerHTML = '<h5><strong>Filename:</strong> '+fileName+'</h5><h4>Uploaded file size is <label style="color:blue;">'+convertBytes(file.size)+'</label></h4>';
  28. }
  29. }

In the code above we will create first a method called convertBytes(), this function will convert the byte size into a compressive format such as kb, gb, mb, etc. And next we will create a method called getFileSize(), in the first line of this function we will bind our file input id so that we can get the actual information of the uploaded file. In order to display the file size to the html page we will just call the convertBytes() function and append the file size of the uploaded file on it.

Output:

The How to get Uploaded File Size Value 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 get Uploaded File Size Value 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