2

I want to create a multiple image uploader inside a form with preview and delete options. I am using php as part of my backend.

The part of the preview is easy. I've followed the explanation in

Image Upload with preview and Delete option - Javascript / Jquery

My problem comes when I want to delete an image. It is deleted from the preview but not from the e.target.files array, so when I access the variable $_FILES in php, I obtain all the images, including the ones removed.

As you can see in the picture, if I remove two images from the initial 5, there is still a message that says that I have 5 files.

I would like to know how can I remove those images completely, so in php I only access the images that have not been removed.

Thanks.

Chris
  • 4,672
  • 13
  • 52
  • 93
RJ-mac
  • 23
  • 4

1 Answers1

1

Form

<form id="myForm" method="post">

Files: <input type="file" id="files" name="files" multiple><br/>

<div id="selectedFiles"></div>

<input type="submit">
</form>

AJAX

 var selDiv = "";
        var storedFiles = [];

        $(document).ready(function() {
            $("#files").on("change", handleFileSelect);

            selDiv = $("#selectedFiles"); 
            $("#myForm").on("submit", handleForm);

            $("body").on("click", ".selFile", removeFile);
        });

        function handleFileSelect(e) {
            var files = e.target.files;
            var filesArr = Array.prototype.slice.call(files);
            filesArr.forEach(function(f) {          

                if(!f.type.match("image.*")) {
                    return;
                }
                storedFiles.push(f);

                var reader = new FileReader();
                reader.onload = function (e) {
                    var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>";
                    selDiv.append(html);

                }
                reader.readAsDataURL(f); 
            });

        }

        function handleForm(e) {
            e.preventDefault();
            var formdata = new FormData();

            for(var i=0, len=storedFiles.length; i<len; i++) {
                formdata.append('files[]', storedFiles[i]); 
            }

            $.ajax({
                    url: "tab.php",
                    type: 'POST',
                    data: formdata,
                    dataType: "json",
                    contentType: false,
                    cache: false,
                    processData: false,
                    success: function(data) {
                        return true;
                    }
                });
        }

        function removeFile(e) {
            var file = $(this).data("file");
            for(var i=0;i<storedFiles.length;i++) {
                if(storedFiles[i].name === file) {
                    storedFiles.splice(i,1);
                    break;
                }
            }
            $(this).parent().remove();
        }
Vaibhavi S.
  • 1,083
  • 7
  • 20
  • How can I obtain the id of the removed image given an input like this? `` – RJ-mac Oct 01 '19 at 10:57
  • @RJ-mac No, When upload image append `data-id` into preview div remove button like `` – Vaibhavi S. Oct 01 '19 at 11:01
  • Ok, but where do I have to put your delete code? Inside the remove click function done in https://stackoverflow.com/questions/37205438/image-upload-with-preview-and-delete-option-javascript-jquery? – RJ-mac Oct 01 '19 at 11:16
  • @RJ-mac `data-id`Add here dynamically like 0,1,2.. `Remove image` – Vaibhavi S. Oct 01 '19 at 11:17
  • @RJ-mac Please See Edited Answer. – Vaibhavi S. Oct 01 '19 at 11:19
  • I've inserted that code into mine and now it is not even removing the preview of the image. – RJ-mac Oct 01 '19 at 11:36
  • @RJ-mac Check by `console.log(appendId)` – Vaibhavi S. Oct 01 '19 at 11:44
  • It prints always the number of images that I have loaded. For example, if I have 5 images and try to delete the first one, it prints 5. – RJ-mac Oct 01 '19 at 11:54
  • `Remove image` in this iis `data-id` same for all remove span ?? – Vaibhavi S. Oct 01 '19 at 11:56
  • I've put `Remove image` just as you've done in your code. But is seems that appendId is always equal to the number of images. – RJ-mac Oct 01 '19 at 12:25
  • @RJ-mac see again my edited code and change. its work for me – Vaibhavi S. Oct 01 '19 at 12:31
  • Hi, it is still not removing the previews.. In addition, now the `appendId` variable is 0 for the last image, 1 for the previous to the last, and so on. I've changed your code putting `var ii = i;` and `ii--` to make `appendId` to refer to the corresponding image but it is not working either. Don't know why it is working for you – RJ-mac Oct 01 '19 at 12:44
  • @RJ-mac Check Edited Complete Code Again. You need to append `filecollection` array with formdata when form submit. and get that image as file from php side. – Vaibhavi S. Oct 01 '19 at 13:05
  • Thank you very much for your replies. Now it deletes well the images. However, when printing variable $_FILES, it only gives me one image. Why is this happening? – RJ-mac Oct 01 '19 at 15:53
  • Solved, I had to change the append of the formdata to `formdata.append('files[]', storedFiles[i]);` – RJ-mac Oct 01 '19 at 16:30
  • Just one more thing. Is there a way to change the label of the button that says "x files selected"? When I remove one image it still remains the same. I want to substract one to that number when an image is removed. – RJ-mac Oct 01 '19 at 16:32