1

Trying to add validation for my files to see if the files being uploaded exceed 5MB, however it doesnt alert if the file is over 5MB, Can anyone see what is wrong with my validaiton in my IF statement? This is a multi file upload which works fine but the validation doesnt

function makeProgress(number){   
          var url = getRelativeURL("web/fileUpload");   
          var formData = new FormData();
          formData.append('number', number);
          fls = document.getElementById("attachmentFileUploadInput").files; //number of files... 
          console.log(fls);
          for(j=0;j<fls.length;j++){
              if (fls.size > 5000000) //5MB
            {
                  console.log('error');
                  alert('file to big');
            }
              else
                  {
              formData.append('files[]', fls[j]);  //note files[] not files
              $.ajax({
                  url : url,
                  data : formData,
1
  • 1
    fls is an array of files? so, to get size of each indivial file:fls[j].size Commented Apr 15, 2017 at 12:19

1 Answer 1

1

You don't check the size of the current file. Change

if (fls.size > 5000000) //5MB

to

if (fls[j].size > 5000000) //5MB

One more think. You don't declare j in your function and therefore it is in the global scope which can break things because it can be manipulated in some other parts of your code. You can declare it like this:

var j;
for(j=0;j<fls.length;j++){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.