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.
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).
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.
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) { ... }
The file size is measured in bytes - do this:
if (s > 4 * 1024 * 1024)
alert("Too big");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With