Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What measuring unit is used for file sizes in javascript?

Tags:

javascript

am trying to validate file size on the client side before uploading to server. However I think i need to calculate max-size in javascript.

So how do i write 4MB in javascript ? I would also want to know basically in what measuring unit is javascript calculated in terms of file size.

below is my colde:

//Grab the file list
        var files   =   e.target.files;
        $.each(files,function(i,file){

        //check for the correct file extensiton
        var n   = file.name,
            s   = file.size
            t   = file.type;
        if(s > 4MB){

            console.log("File is greater than 4MB");

   }
}

Please help.Thank you.

like image 205
lernyoung Avatar asked Sep 03 '16 09:09

lernyoung


People also ask

How is a file size measured?

File sizes are measured in Bytes (B), Kilobytes (KB), Megabytes (MB), Gigabytes (GB), Terabytes (TB) and so on. The file sizes can be measured using a binary system (where kilo means 1024) or metric system (kilo means 1000).

What do you call the size of a file?

Bytes are the typical base unit of information. Larger files will typically have their sizes expressed using kilobyte, megabyte or gigabyte depending upon how large the file is.

How do I convert MB to file size?

zip"); // Get length of file in bytes long fileSizeInBytes = file. length(); // Convert the bytes to Kilobytes (1 KB = 1024 Bytes) long fileSizeInKB = fileSizeInBytes / 1024; // Convert the KB to MegaBytes (1 MB = 1024 KBytes) long fileSizeInMB = fileSizeInKB / 1024; if (fileSizeInMB > 27) { ... }


1 Answers

The file size is measured in bytes - do this:

if (s > 4 * 1024 * 1024)
    alert("Too big");
like image 83
Jimmy Thomsen Avatar answered Sep 20 '22 10:09

Jimmy Thomsen