Remove a FileList item from a multiple “input:file”

后端 未结 5 1137
生来不讨喜
生来不讨喜 2020-12-01 16:06

I have this DOM:

var id = 0;

$(\'input:file#upload\')[0].files[ id ]

That get\'s the first file on the 0th index. File properties are list

5条回答
  •  旧时难觅i
    2020-12-01 16:59

    No, We can make it removable. I implemented this and it works definitely.

    First you need to initialize this variables

    var newImageObj = [];
    var ImageNo = 0;
    

    Then write this code on file input's change

    $("#exampleInputFileProduct").change(function () {
    
                var fileUpload = document.getElementById("exampleInputFileProduct");
    
                //$("#mainImages").html('');
                //$("#subImages").html('');
    
                if (typeof (FileReader) != "undefined") {
    
                    //Here Check File Extension
                    var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png)$/;
    
    
                    for (var i = 0; i < fileUpload.files.length; i++) {
                        var j = 0;
                        var file = fileUpload.files[i];
                        var NewFile = fileUpload.files[i];
                        //Here Check File Size 1MB = 1000000 Bytes
                        if (file.size < 2048000) {
                            if (regex.test(file.name.toLowerCase())) {
                                var reader = new FileReader();
                                reader.onload = function (e) {
    
                                    if ($("#mainImages").find(".item").attr("id") == "FirstSlider") {
                                        $("#mainImages").html('');
                                        $("#subImages").html('');
                                        $("#subImages").append("
    "); } if ($("#mainImages").find(".item").hasClass("active")) { $("#mainImages").append("
    "); } else { $("#mainImages").append("
    "); } //if ($("#subImages").find(".item").length == 0) { // $("#subImages").append("
    "); //} else { if (($("#subImages").find(".item").find("div").length / 5) >= $("#subImages").find(".item").length) { $("#subImages").append("
    "); } //} var append = 0; $.each($("#subImages").find(".item"), function (p, pelement) { if (append == 0) { if ($(pelement).find("div").length != 5) { var newID = $(pelement).find("div").length; newID = newID; $(pelement).append("
    "); append = append + 1; } } }) j = j + 1; ImageNo = ImageNo + 1; } newImageObj.push(file); reader.readAsDataURL(file); } } } } else { alert("This browser does not support HTML5 FileReader."); } });

    Then at last this 2 functions will help to do the rest

    function LoadImage(objclass) {
                $("#mainImages").find(".item").removeClass("active");
                $("#mainImages").find("." + objclass + "").addClass("active");
            }
    
            function RemoveImage(objclass, ImageName) {
    
                $.each(newImageObj, function (e, element) {
                    if ($(this)[0].name.toLowerCase().trim() == ImageName.trim()) {
                        newImageObj.pop(this);
                    }
                });
    
                $("#mainImages").find("." + objclass + "").remove();
                $("#subImages").find(".item").find("." + objclass + "").remove();
    
                if ($("#mainImages").find(".item").length == 0) {
                    $("#mainImages").append("
    "); $("#subImages").append("
    "); } else { $("#mainImages").find(".item").removeClass("active"); $("#mainImages").find(".item:first-child").addClass("active"); $("#subImages").find(".item").removeClass("active"); $("#subImages").find(".item:first-child").addClass("active"); } }

    At last when you submit your form than take the files from the array

提交回复
热议问题