OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get total size of combined uploaded files with JQuery/Javascript

  • Thread starter Thread starter Blawless
  • Start date Start date
B

Blawless

Guest
I have a page where a user can upload a document. There is also a button to add more documents (as seen per image).

Dynamicaly added upload points

after adding upload button

I want to check as each file that has been added or removed from the upload section to make sure that the size does not exceed 10 MB.

So if a user uploads 3 separate files of 2 MB and then adds a file which is 5 MB, the submit button will disable until one of the files are removed because it has gone over the 10 MB limit.

The rows can be added dynamically, so I was doing it by class and trying to sum the running total.

Here is what I have so far, available at JFIDDLE

Code:
<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 issue is, that if I add a file and then remove it, it does not reflect the current running total.

Code:
$("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 tried this way also, but could not reset when a file was removed.JFIDDLE-2

<p>I have a page where a user can upload a document. There is also a button to add more documents (as seen per image).</p>

<p><a href="https://i.sstatic.net/rz5if.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rz5if.png" alt="Dynamicaly added upload points"></a></p>

<p><a href="https://i.sstatic.net/me2DG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/me2DG.png" alt="after adding upload button"></a></p>

<p>I want to check as each file that has been added or removed from the upload section to make sure that the size does not exceed 10 MB.</p>

<p>So if a user uploads 3 separate files of 2 MB and then adds a file which is 5 MB, the submit button will disable until one of the files are removed because it has gone over the 10 MB limit.</p>

<p>The rows can be added dynamically, so I was doing it by class and trying to sum the running total.</p>

<p>Here is what I have so far, available at <a href="http://jsfiddle.net/9bhcB/1210/" rel="nofollow noreferrer">JFIDDLE</a></p>

<pre><code><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>
</code></pre>

<p>The issue is, that if I add a file and then remove it, it does not reflect the current running total.</p>

<pre><code>$("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);
totalSize = totalSize + fileArr;
}

if (totalSize > 10485759) {
alert('Over Max Size');
$(submitBtn).prop("disabled",true);
}

});
});
</code></pre>

<p>I tried this way also, but could not reset when a file was removed.<a href="http://jsfiddle.net/9bhcB/1211/" rel="nofollow noreferrer">JFIDDLE-2</a></p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top