How to make the Multiple file input field in a second selection keep the previously selected files?

Asked

Viewed 2,051 times

0

When Open the file selection window it lets me select multiple files, OK!.

But after opening the files if user click again to open more files it clears the previous selection... How to make him leave the previous selection and add the new selection ?


<input type="file"  name="imagem[]" id="imagem[]" class="form-control" multiple="multiple" >

1 answer

1

I think you could use an Event Listener as in the example I made below. So, in each new input, you add a File, or more, in the Array inputFiles.

By clicking on Remove File is deleted from array.

Then just send the back end to Array inputFiles.

var inputFiles = [];

function newInput(input) {
  var filesStr = "";

  for (let i = 0; i < input.files.length; i++) {
    inputFiles.push(input.files[i]);
    filesStr += "<li>" + input.files[i].name + "<button onclick='removeLi(this)'>Remover</button>" + "</li>";
  }

  document.getElementById("file-input").value = "";

  document.getElementById("dp-files").innerHTML += filesStr;
}

function removeLi(e) {
  inputFiles = inputFiles.filter(function(file) {
    return file.name !== e.parentNode.innerHTML.split("<button")[0];
  })
  e.parentNode.parentNode.removeChild(e.parentNode);
}
<form id='form' action='' method='post' enctype='multipart/form-data'>

  <input id='file-input' class='file-input' type='file' name='file' onchange="newInput(this)" multiple='multiple' />

</form>

<ul id="dp-files"></ul>

  • Thanks for your help.. but I need it to show auto without the "show files" button and also an individual delete button per selected file.

  • @Richardbarcelos - As you requested I edited the original response by adding a file delete and storage system into an array.

  • Beauty. More question arose rsrsr Sorry. How would I get the inputFiles array? if I give a print_r in the array it will return me? I was confused in this part, I usually give a post['']; if you can give me this hint :)

  • Take a look and see if this other question helps you :D https://stackoverflow.com/questions/45531155/send-array-of-files-using-ajax

  • I’m not using json to pick up or pass the data, I’m using codeigniter I tried to get it so already in my controller $files = $_FILES['inputFiles']; but without success

  • I have a form with this input file and other imputs that tbm need to send to my action - they are sending, but in the other page I receive them and the one that mounted not...

  • I’m sorry that I won’t be able to help you beyond here, because I don’t work with php :(( Until I think it would be interesting to open another specific question about this other question or see if someone can help you here in the comments. Anyway I wish you success in the search

  • Thanks for the help. I already did a search and it seems that I have to play the images of this array in one of the inputs that I am submitting in the form.. I’m figuring out how to do this too. Thanks again.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.