Check size of photograph using JavaScript

41 Views Asked by At

I want to use JavaScript with SuccessFactors to check if the size of an uploaded photo is greater than 2 MB.

I wrote the following JavaScript code, but it does not seem to be working:

if (context.files.length > 0) {
    for (const i = 0; i <= context.files.length - 1; i++) {
        const fsize = context.files.item(i).size;
        const file = Math.round((fsize / 1024));

        if (file >= 4096) {
            alert("File too Big, please select a file less than 4mb");
        }
    }
}
1

There are 1 best solutions below

1
On

You can use for of to loop through files list

if (context.files.length > 0) {
          for (let file of context.files) {
            const size = Math.round(file.size / 1024);
            if (size >= 4096) {
              alert("File too Big, please select a file less than 4mb");
            }
          }
        }