Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var counter = 0;

            var arr = new Array();

            function addFUControl() {
                
                var visible = $('input[type=file]').filter(function () {
                    return !($(this).css('visibility') == 'hidden' || $(this).css('display') == 'none');
                }).length;

                if (visible <= 1) {
                    for (var i = 0; i <= 4; i++) {
                        AddFileUpload();
                    }
                }
                else {

                }
            }


            function AddFileUpload() {
                var div = document.createElement('DIV');
                div.innerHTML = '<input id="file_' + counter + '" name = "file' + counter +
                 '" type="file" Class = "fileUploadCntrl" />';

                document.getElementById("FileUploadContainer").appendChild(div);


                arr[counter] = document.getElementsByName("file" + counter);
                counter++;

                //document.getElementById("FileUploadContainer").removeChild(div);

            }


            function FileUploadClick() {

                $find('<%= modeloPopUpFileUpload.ClientID%>').hide();
                debugger;
                for (var i = 0; i < arr.length; i++) {
                    var previousFilePath = $(arr[i - 1]).val();                    
                    var filepath = $(arr[i]).val();

                    if (filepath != "")
                    {
                        if (filepath != previousFilePath) {
                            var Button = '<input id="btn_' + i + '" name = "btn' + i +
                                         '" type="button" text="Delete" Class = "btn" style=width:80px; />';

                            $("#SelectedFilesManuplation").append(filepath);
                            $("#SelectedFilesManuplation").append(Button);
                            $("#SelectedFilesManuplation").append('<br>')                            
                            $("#file1Flag").val("1")
                        }
                        var a = arr[i];
                        $(a).hide();
                    }
                    else if (filepath == "")
                    {
                        var p2 = arr[i];
                        $(p2).remove();
                    }


These are my functions.

when User click on btn_AddFileUploadControl a popup get open and call the function addFUControl().

and the function create 5 dynamically FileUpload controls in the popup.

User select file and then press btn_OK. on btn_OK click it call the function FileUploadClick().

and this close the popup and display the filepath on my page.

hide the controls which have value.and remove those controls which don't have files.

MY PROBLEM IS THIS....


suddenly my user want to add more file.when he click on btn_AddFileUploadControl the popup get open and user select 1 more file.

and then he press btn_OK.
the files/controls which got hidden on btn_OK click last time.
are repeating again in the page.

for example.first time user select
1: c:/>test1.text
2: c:/>test2.text
these two files.and these file path displaying on page.

and when he click on again btn_AddFileUploadControl and from popup he select one more file

3: c:/>test3.text

when he press btn_OK then on my page it is displaying repetition.
like this one

c:/>test1.text
c:/>test2.text
c:/>test1.text
c:/>test2.text

c:/>test3.text


but i dont want this.

i want to show

c:/>test1.text
c:/>test2.text
c:/>test3.text



please help me in this problem.
Posted

1 solution

JavaScript
//Code By RANA RUMEEL For The File Upload Functionality...
            
            var counter = 0;
            var arrFileUploadIDs = new Array();
            var arrFileUploadPaths = new Array();

            //This function papulate the dynamic controls in the FilesUpload Popup.And handel controls functionality
            function createFileUploadControl() {
                var visibleControls = $('input[type=file]').filter(function () {
                    return !($(this).css('visibility') == 'hidden' || $(this).css('display') == 'none');
                }).length;

                if (visibleControls <= 1) {
                    for (var i = 0; i <= 4; i++) {
                        CreatePopupControl();
                    }
                }
                else {
                }
            }

            function CreatePopupControl()
            {
                var div = document.createElement('DIV');

                div.innerHTML = '<input id="file_' + counter + '" name="file' + counter +<br mode=" hold=" />                 '" type="file" class="fileUploadCntrl" />';

                document.getElementById("FileUploadContainer").appendChild(div);
                arrFileUploadIDs[counter] = document.getElementsByName("file" + counter);
                counter++;
            }

            //This function get call from the button upload file.And it create dynamic delete button and the File Paths
            function FileUploadClick() {
                $find('<%= modeloPopUpFileUpload.ClientID%>').hide();
                var table = "<table id="tblFileUpload">"

                for (var i = arrFileUploadIDs.length - 6; i < arrFileUploadIDs.length; i++) {
                    var filepath = $(arrFileUploadIDs[i]).val();

                    if (filepath != "") {
                        if (!checkUniqueFileNamePath(filepath)) {
                            var emptyControl = arrFileUploadIDs[i];
                            $(emptyControl).remove();
                        }
                        else {
                            var dynamicButton = "<input id="btn_"" i="" mode="hold" />                                         "' type='button' Value='Delete' Class = 'btn' onclick='RemoveFileUpload(\"tr_" + i + "\", this);' style=width:80px; />";

                            table = table + "<tr id="tr_"" i="" filepath="" dynamicbutton="" mode="hold" />                            var hideControl = arrFileUploadIDs[i];
                            $(hideControl).hide();
                        }
                    }
                    else if (filepath == "") {
                        var emptyControl = arrFileUploadIDs[i];
                        $(emptyControl).remove();
                    }
                }
                table = table + '</table>';
                $("#SelectedFilesManuplation").append(table);
            }

            //This function stop the duplication in the uploaded files...
            function checkUniqueFileNamePath(filePath) {
                for (var i = 0; i < arrFileUploadIDs.length; i++) {
                    if (filePath == arrFileUploadPaths[i]) return false;
                }
                arrFileUploadPaths[arrFileUploadPaths.length] = filePath;
                return true;
            }

            // To Remove the dynamically created control and text from Page...
            function RemoveFileUpload(trID, btn) {
                btn.parentElement.parentElement.parentElement.removeChild(document.getElementById(trID));
            }
            //End code rumeel

this is the solution.i was working and geting late because no one like to reply here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900