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.
– Richard Barcelos
@Richardbarcelos - As you requested I edited the original response by adding a file delete and storage system into an array.
– Arthur Ferreira
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 :)
– Richard Barcelos
Take a look and see if this other question helps you :D https://stackoverflow.com/questions/45531155/send-array-of-files-using-ajax
– Arthur Ferreira
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
– Richard Barcelos
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...
– Richard Barcelos
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
– Arthur Ferreira
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.
– Richard Barcelos