0

I used the below code to upload multiple files. Its working absolutely fine but as i need to check that the file which i am uploading is duplicate or not, i am facing one problem in that. I created one function called checkDuplicate for that and calling it inside the function. But the problem is that the for loop is looping double the size of the array. I don't know why it is so. Please kindly help me if anyone has any idea.

Here is the Javascript

<script type="text/javascript">

function MultiSelector(list_target, max) {
    this.list_target = list_target;
    this.count = 0;
    this.id = 0;
    if (max) {
       this.max = max;
    } else {
       this.max = -1;
};

this.addElement = function(element) {

if (element.tagName == 'INPUT' && element.type == 'file') {
    element.name = 'file_' + this.id++;
    element.multi_selector = this;
    element.onchange = function() {
    var new_element = document.createElement('input');
    new_element.type = 'file';
    this.parentNode.insertBefore(new_element, this);
    this.multi_selector.addElement(new_element);
    this.multi_selector.addListRow(this);
    this.style.position = 'absolute';
    this.style.left = '-1000px';
};

if (this.max != -1 && this.count >= this.max) {
    element.disabled = true;
}
;
this.count++;
this.current_element = element;
} 
else {
     alert('Error: not a file input element');
}
;
};

this.addListRow = function(element) {
     var new_row = document.createElement('div');
     var new_row_button = document.createElement('img');
     new_row_button.setAttribute("src","<%=request.getContextPath()%>/images/deletei.gif");
     new_row_button.onclick = function() {
     this.parentNode.element.parentNode.removeChild(this.parentNode.element);
     this.parentNode.parentNode.removeChild(this.parentNode);
     this.parentNode.element.multi_selector.count--;
     this.parentNode.element.multi_selector.current_element.disabled = false;
     return false;
}; 
if(checkDuplicate(element)) {
   new_row.element = element;
   new_row.innerHTML = element.value + "&nbsp;&nbsp;";
   new_row.appendChild(new_row_button);
   this.list_target.appendChild(new_row);
}
};
};
function checkDuplicate(element) {
    var arr = new Array();
    var i = 0,dup=0;
    //alert(new_row.element = element.value);
    if(dup==0) {
       arr[i++] = element.value;
       dup=1;
    }
    alert("Length ==> "+ arr.length);
    for ( var j = 0; j < arr.length; j++) {
         alert("Name ==> " + arr[j]);
         if(arr[j] == element.value && j>=1) {
            alert("Duplicate");
         } else {
           alert("Not Duplicate");
           arr[i++] = element.value;
         }
    }
}
</script>

Here is the HTML

<body>
<!-- This is the form -->
<form enctype="multipart/form-data" action=""method="post">
<input id="my_file_element" type="file" name="file_1">
<input type="submit">
<br/>
<br/>
Files:
<!-- This is where the output will appear -->
<div id="files_list"></div>

</form>
<script>
var multi_selector = new MultiSelector(document
.getElementById('files_list'), 15);

multi_selector.addElement(document.getElementById('my_file_element'));
</script>
</body>
</html>

1 Answer 1

1

because you have the arr[i++] = element.value; in the last line, and j < arr.length in the for, so every time the array.lenght gets bigger and bigger.

change the for line to these two lines:

var len = arr.length;
for ( var j = 0; j < len; j++) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.