1

$(document).ready(function () {
  $(document).on('click', "button", function (e) {
      $(this).closest('tr').remove();
  });
});
var filelist = new Array();

updateList = function () {
  var input = document.getElementById('fileUploader');
  var output = document.getElementById('divFiles');

  var HTML = "<table>";
  for (var i = 0; i < input.files.length; ++i) {
      filelist[i] = input.files.item(i).name;
      HTML += "<tr><td>" + filelist[i] + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button ></button></td></tr>";
  }

  HTML += "</table>";
  output.innerHTML = HTML;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="form-group col-md-2 col-sm-2">
	<label class="label1">
		<img src="~/Images/upload.png" height="50px" width="50px" /> Upload Your File
		<input type="file" name="fileUploader" id="fileUploader" multiple onchange="javascript:updateList()" />
	</label>
</div>
<br />

In the code I am uploading multiple files. When I am trying to upload some more files the existing file list replaced with new list. Even after placing my array outside the function. enter image description here

3
  • Please share your html & also show who is calling updateList function Commented Apr 23, 2018 at 7:28
  • You replace the array elements by adding them with your index key that starts from zero: filelist[i] = input.files.item(i).name; instead of using something like push. It means that if your first selection has more files than your newest, the last files from the first selection are still in the array, am i right? Commented Apr 23, 2018 at 7:29
  • <div class="form-group col-md-2 col-sm-2"> <label class="label1"> <img src="~/Images/upload.png" height="50px" width="50px" /> Upload Your File <input type="file" name="fileUploader" id="fileUploader" multiple onchange="javascript:updateList()" /> </label> <br /> Commented Apr 23, 2018 at 7:30

4 Answers 4

2

If you want to preserve the previously added file names, you should push the file names into filelist.

var filelist = new Array();

updateList = function () {
    var input = document.getElementById('fileUploader');
    var output = document.getElementById('divFiles');

    var HTML = "<table>";
    for (var i = 0; i < input.files.length; ++i) {
        //filelist[i] = input.files.item(i).name;
        filelist.push(input.files.item(i).name);
        HTML += "<tr><td>" 
              + filelist[i] 
              + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button ></button></td></tr>";
    }
    HTML += "</table>";
    output.innerHTML = HTML;
    console.log(filelist);
}
<input id="fileUploader" type="file" multiple="multiple" onchange="updateList()">
<div id="divFiles"></div>

Sign up to request clarification or add additional context in comments.

4 Comments

its not working as we expect when i am trying to upload a new set o file it showing old list file names it self.
So your expected output is to append new file names to filelist and show new file names list in output?
yes the new file names need to get stored in the filelist and list in output
displaying in console but not html output. also when i upload 2 files first time then i am trying to one more file means. from the existing list one file got removed.
1

Please try this,

 <script>
    $(document).ready(function () {
        $(document).on('click', "button", function (e) {
            $(this).closest('tr').remove();
        });
    });
   </script>
   <script>
       var filelist = new Array();

       updateList = function () {
           var input = document.getElementById('fileUploader');
           var output = document.getElementById('divFiles');

           var HTML = "<table>";
           for (var i = 0; i < input.files.length; ++i) {
               filelist[i]=input.files.item(i).name;
               HTML += "<tr><td>" + filelist[i] + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button ></button></td></tr>";
           }
           HTML += "</table>";
           output.innerHTML += HTML;
       }
   </script>

Comments

1

Use an array to save all files, Each time user select new files, push them into array.

var fileList = [];

in updateList function fill array

fileList.push(...);

Comments

1

var filelist = new Array();

updateList = function () {
    var input = document.getElementById('fileUploader');
    var output = document.getElementById('divFiles');

    var HTML = "<table>";
    for (var i = 0; i < input.files.length; ++i) {
        //filelist[i] = input.files.item(i).name;
        filelist.push(input.files.item(i).name);
        HTML += "<tr><td>" 
              + filelist[i] 
              + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button ></button></td></tr>";
    }
    HTML += "</table>";
    output.innerHTML = HTML;
    console.log(filelist);
}
<input id="fileUploader" type="file" multiple="multiple" onchange="updateList()">
<div id="divFiles"></div>

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.