0

I found this solution on this question. I want to get the image weight from a file input. On the solution, the result is brought with the MiB (Mebibyte) unit. I wanted to know if is there some way to bring the image weight, with the same code, but on another unit, such as Megabytes.

The solution:

$('#file-input').bind('change', function() {
  alert('This file size is: ' + this.files[0].size/1024/1024 + "MiB");
});

Heres an example of how I'm doing it:

$('#file-input').bind('change', function() {
  alert('This file size is: ' + this.files[0].size/1024/1024 + "MiB");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="file" name="" id="file-input">

As you can see, the unit is brought in Mebibytes. How can I change that?

Cesar Pc
  • 27
  • 5

1 Answers1

1

this.files[0].size returns the size in bytes

and /1024/1024 converts bytes to mebibytes (1/2^20 bytes). Simply change this to /1000000 to convert bytes to megabytes (1/10^6 bytes)

jgtokyo
  • 188
  • 5