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++); } }}
right now it doesn't change the name or id and it add "submitName to my input file field.
Julian Hansen
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; } }}
If I add or remove it should update the file names
hielo
>> 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)?
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?
Julian Hansen
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>
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
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.
Open in new window