dynamic file input controls

J

J.R

Greetings,

I'm adding dynamically created input type='file' controls via JavaScript.
However when I try to access they do not seem to be returned in the form
collection. Any ideas?

Thanks, John.
 
V

VK

You cannot set/access file properties from JavaScript : the access is locked
for security reasons.
 
I

Ivo

J.R said:
Greetings,

I'm adding dynamically created input type='file' controls via JavaScript.
However when I try to access they do not seem to be returned in the form
collection. Any ideas?
Thanks, John.

Are you sure they have unique names, or if not, unique indeces in an array?
Here is what I use, more or less directly copied from the page so it is not
tailored to the question:

<script type=text/javascript>
function add(n){
curdiv=document.activeElement.previousSibling;
newdiv=curdiv.cloneNode(true);
curdiv.insertAdjacentElement("afterEnd",newdiv);
}
</script>

<form method=post enctype="multipart/form-data" action="/script.htm">
<input type=hidden name=MAX_FILE_SIZE value=3000000>
<div>File: <input type=file name=userfile[]></div>
<input class=button type=button value="Add" onclick=add(this) ><br>
<input class=button type=submit value=" Upload ">
</form>


There are several forms on this page, each with their own "Add" button, and
in the case of the upload form with the <input type=file>,
activeElement.previousSibling (not sure if this is IE only) is the div
containing both the input and its label. Note the input name is a member of
an array because of the square brackets. This ensures the server script that
receives the form submission will see all inputs, regardless of how many
times I clicked "Add"!

HTH
Ivo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,226
Latest member
uatas12

Latest Threads

Top