I have a page where the user can upload a document. There is also a button for adding additional documents (as seen in the image).


I want to check how each file that has been added or removed from the download section to make sure the size does not exceed 10 MB.
So, if the user uploads 3 separate files of 2 MB in size, and then adds a file of 5 MB in size, the submit button will be disabled until one of the files is deleted, because he switched to the 10 MB limit.
Lines can be added dynamically, so I did it by class and tried to sum the current amount.
Here is what I still had in JFIDDLE
<form class="upload-form"> <input class='upload-file' type="file" id="upload0"> <input class='upload-file' type="file" id="upload1"> <input class='upload-file' type="file" id="upload2"> <input class='upload-file' type="file" id="upload3"> <input id='submitBtn' type=submit> </form>
The problem is that if I add a file and then delete it, it will not reflect the current current amount.
$("document").ready(function() { var fileArr = []; $(".upload-file").on('change',function() { var fileInput = $('.upload-file'); var fileSize = fileInput.get(0).files[0].size; fileArr.push(fileSize); var totalSize = 0; for (var i in fileArr) { alert(fileArr[i]); totalSize = totalSize + fileArr[i]; } if (totalSize > 10485759) { alert('Over Max Size'); $(submitBtn).prop("disabled",true); } }); });
I also tried this path, but could not reset when the file was deleted. JFIDDLE-2
source share