Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Reorder multi file names

Hi,

I have a javascript that allow the user to upload multiple files and removes files.

My code will add the files starting from 0 - what ever number of files need to upload.

for example:
name=file0
name=file1
name=file2
name=file3
name=file4

if I remove two of the uploaded files (file2, file3)
I want the javascript to rename my files and give me the total number of upload.

so the output should be:
name=file0
name=file1
name=file2

Result = 3 for the total number of uploaded files.

I removed two of the files and renamed them from 0 - 2


this is my code

var counter = 0;

function AddFileUpload()

{

     var div = document.createElement('DIV');

     div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + 

                     '" type="file" />' +

                     '<input id="Button' + counter + '" type="button" ' + 

                     'value="Remove" onclick = "RemoveFileUpload(this)" />';

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

     counter++;

}

function RemoveFileUpload(div)

{

//here I need to rename my input files because I removed one and get the total count of the uploaded files.

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

}

     <table style="width:100%;border:1px solid #eeeeee;background-color:#f5f5f5;">
           <tr>
<td align="right" valign="top" style="width:10%;padding-right:5px;"><span style="color:#4a79a5;font-weight:normal;"> <span style="color:#4a79a5;font-weight:normal;"> Click to add files:</span></td>
                <td valign="top" colspan="6">                   
                     <input id="Button1" type="button" value="add" onclick = "AddFileUpload()" />
    <br /><br />
    <div id = "FileUploadContainer">
        <!--FileUpload Controls will be added here -->
    </div>
    <br />
                </td>
              </tr>    
        </table>

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Try this
function RemoveFileUpload(div)
{
//here I need to rename my input files because I removed one and get the total count of the uploaded files.
  var el = document.getElementById("FileUploadContainer");
  el.removeChild(div.parentNode); 
  var children = el.getElementsByTagName('input');
  var indx = 0;
  for (var i = 0; i < children.length;i++) {
    if (children[i].type.toLowerCase() == 'file') {
      children[i].setAttribute('name', 'file' + indx++);
    }
   }
}

Open in new window

Avatar of lulu50

ASKER

Hi Julian,

Thank you for helping me on this.

I am getting this output

<input name="file1" id="file1" submitName="file0" type="file"/>

what i want is for the name and id to change so the output should look like this:

<input name="file0" id="file0" submitName="file0" type="file"/>

right now it doesn't change the name or id and it add "submitName to my input file field.
Fairly straight forward - create the name and then assign to the children\[i\].id member like so.
function RemoveFileUpload(div)
{
//here I need to rename my input files because I removed one and get the total count of the uploaded files.
  var el = document.getElementById("FileUploadContainer");
  el.removeChild(div.parentNode); 
  var children = el.getElementsByTagName('input');
  var indx = 0;
  for (var i = 0; i < children.length;i++) {
    if (children[i].type.toLowerCase() == 'file') {
      var newname = 'file' + indx++;
      children[i].setAttribute('name', newname);
      children[i].id = newname;
    }
   }
}

Open in new window

right now it doesn't change the name or id and it add "submitName to my input file field.
Then you need to post code to show how you have implemented. Code does work - refer sample below

http://www.marcorpsa.com/ee/t921.html
What browser are you using? There was a known bug in IE that resulted in submitname being created - but that was IE7.

I have updated the sample to dump the contents of the input's to a textarea so you can view them there. Tested in IE8,10,11 - seems to work fine?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

I am unsing IE 9

How can I get the total number of uploaded files?
Avatar of lulu50

ASKER

when I test your example I see the correct output but when I bring your code to my page I get this result


<!--FileUpload Controls will be added here -->
<DIV><INPUT id=file0 name=file2 type=file><INPUT id=Button2 onclick=RemoveFileUpload(this) value=Remove type=button></DIV>
<DIV><INPUT id=file1 name=file3 type=file><INPUT id=Button3 onclick=RemoveFileUpload(this) value=Remove type=button></DIV>

Open in new window


file id is the only one that is getting updated
Avatar of lulu50

ASKER

If I add or remove it should update the file names
>> the rename is on the id and name fields only - not sure how that relates to the actual file attached? Maybe I did not understand your question?
What I meant was, does the browser honor the name of the field when it was "attached/selected", or does it honor the name at the time of submission? To clarify, if there are four <input type="file"/> initially (named file0...file3).  If I then attach a file on all four.  but just before submission I remove the second one only (ultimately uploading only three total).  Does the server see file0,file2,file3 (the original name sequence), or does it see file0,file1, file2 (the new name sequence)?
Avatar of lulu50

ASKER

Hielo,

the server should see the new file0,file1,file2

and provide the correct count so i can do my loop
@hielo,
What I meant was, does the browser honor the name of the field when it was "attached/selected", or does it honor the name at the time of submission
At the time of submission. If you submit the form on the example page you will see that the file names come back

file id is the only one that is getting updated
Tested in IE - works as expected (IE11 and emulation on Edge, 10, 9, 8 and 7) - all worked until IE7.
Tested on genuine XP IE8 - also worked.
What browser version are you using?

and provide the correct count so i can do my loop
What are you using on the backend?
This gives the same results in IE7 but works everywhere else - it uses JQuery instead of straight javascript. See if this makes a difference to your code
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
function AddFileUpload()
{
 ...
}

function RemoveFileUpload(div)
{
	var el = $('#FileUploadContainer');
	$(div).parent().remove();
	counter = 0;
	$('input[type="file"]', el).each(function() {
		var newname = 'file' + counter++;
		$(this).attr({name: newname, id: newname});
	});
}
</script>

Open in new window

Avatar of lulu50

ASKER

Thank you
lulu50,
While I do appreciate your generosity with the points, in the future, please take the time to assess the responses.  Your question was really addressed by Julian.  Julian actually solved the problem, so the Accepted solution should have been once of his posts.  At most, my post should have been an "Assist".  However, in this case I feel that I did not make any "valuable" contribution to even merit an "assist".  I would have preferred for him to get the full credit.

Regards,
Hielo
Avatar of lulu50

ASKER

Hielo - I gave you credit because you recommend using server side which I did and solved the issue I was having.

Julian - I gave you credit because your solution is correct but for some reason I couldn't get to work when I put your code in my files. So, I had to do it using server side instead javascript.

I don't know why the example that you provided works fine on your end but when I bring in the same code to my page, I am getting the wrong result.

So therefore, I split the point.

Thanks,
Lulu