Categories
JavaScript Answers

How to check file size using HTML5 and JavaScript?

Spread the love

Sometimes, we want to check file size using HTML5 and JavaScript.

In this article, we’ll look at how to check file size using HTML5 and JavaScript.

How to check file size using HTML5 and JavaScript?

To check file size using HTML5 and JavaScript, we can use a file input and check the file’s size after selecting the file.

For instance, we write

document.getElementById("fileInput").onchange = () => {
  const fileSize = document.getElementById("fileInput").files[0].size;
  console.log(fileSize);
};

to select the file input with getElementById.

Then we set its onchange property to a function that gets the first selected file with

document.getElementById("fileInput").files[0]

when we select it.

And then we get the file’s size in bytes with size.

Conclusion

To check file size using HTML5 and JavaScript, we can use a file input and check the file’s size after selecting the file.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *