Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:38:14+00:00 2026-06-04T06:38:14+00:00

I used the below code to upload multiple files. Its working absolutely fine but

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:38:15+00:00Added an answer on June 4, 2026 at 6:38 am

    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++) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using the below code to upload multiple images, take the image file name,
My below code works fine and is used to populate a <select> item with
The code below shows a sample that I've used recently to explain the different
Below is my code which is used to upload images and then create some
I used the code below to fetch the image from a url but it
I used the codes(html+php script) below to upload image file submit.html <html> <body> <form
I'm trying to upload a file from Android (2.3.4) using the below code, I
I have used below code to attach quckboook events // Subscribe to UI events...
I had used the below code to get the row index of the UIPickerView
I have some problem with my cfml website. I have used the below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.