Locating File Upload Controls with JavaScript
In JavaScript there is no way of getting a direct handle on File Upload Controls via the Document Object Model (DOM) unless you know their "name" (ID).
Unless you are using R5, where you can specify the elements ID in the HTML attributes of the Upload Element, or you are generating the Upload using pass-thru HTML then you will have to do it this way as there is no way of predicting what domino is going to call any given upload.
The function loops through all elements on a form until it finds one of type "file" and then returns the zero-based position of it within the forms collection of elements.
Test it using the JavaScript function below, after entering a value in the file upload:
<script type="text/javascript">
<!--
function numberOfFileUpload( f ) {
for(var i = 0; i < f.elements.length; i ++) {
if( f.elements[i].type=='file' ){
return i;
}
}
}
// -->
</script>
JavaScript:alert('Value = ' + document.forms[0].elements[numberOfFileUpload(document.forms[0])].value);
Note: If you have multiple upload controls on the same form then use this function instead. It will fill an array with the positions of each upload:
<script type="text/javascript">
<!--
//create global array
var FUCs = new Array{};
function numberOfFileUploads( f ) {
for(var i = 0; i < f.elements.length; i ++){
if( f.elements[i].type=='file' ){
//append position to upload array
FUCs[FUCs.length] = i;
}
}
}
-->
</script>
Feedback
Add your own response here:
Although your email address isn't required it is protected from spambots if you choose to provide it and not to hide it. My right to remove commercial, irrelevant or posts I just don't like is reserved.
Navigate other articles in the category "JavaScript"
| « Previous Article | Next Article » | |
| Limit the types of files users can upload | Copying text from a document to the clipboard |

The other way around
To get the value of the FUC is nice, but it would be great to set the value of it. I think it is impossible, but maybe somebody has got a solution.
I want to use it to automatically upload a Word-document in the (VB) document_save-event by calling an URL that opens a form with a FUC on it, then set the FUC's with the location of the Word-document and then submit the form.
I hope somebody kan help me with this one.
Reply to this comment
Clear the upload value
Hey, Jake! Thanks for the info! I'm using this to be able to attach two images to be shown in two different areas of a page. I'm using a couple of hidden fields to do it, as well as a pile of javascript. I have a little problem, though. I entered validation to make sure that the image is the proper format (i.e. "gif" or "jpg"), and not too big (200x200). I am only missing ONE thing! I can't seem to clear the upload value (i.e. set it to ""). I've tried several different ways. The following little "for" loop should clear all the values from the uploads, bit it doesn't:
for(var i = 0; i < f.elements.length; i++) { if( f.elements[i].type=='file' ){ f.elements[i].value = ""; } } }
What's odd is that if I try
alert( f.elements[i].value);
after the "if", the proper value pops up!
Is there another way? I tried naming the IDs, (i.e. f.elements["Logo"].value = "";) but that didn't work either. Any help would be great.
Thanks, ahead of time!
Steve in Montreal =8P
Reply to this comment
Re: Clear the upload value
Hi Steve,
You can't change the value of the file upload in JavaScript. Even if it's only to make it "".
Why? Security reasons!
Jake
Reply to this comment
Show the rest of this thread
Re: Clear the upload value
You can put the FUC in a <div> and then use innerHTML to clear the div's contents and refill it with FUC's that have value="" set in the html.
Reply to this comment
Show the rest of this thread
Re: Clear the upload value
You should be able to clear the field with your code but also refresh the page with the second line below. This should work and you should not lose previous entered data on the form.
f.elements[i].value = ""; _doClick('$Refresh', this, null, null);
Reply to this comment
How to process files which are on the control
This is greate! I was looking for a code like this a long time ago! So, now, we have the file list do upload! How can i do to copy that files to a server, with java????
Does anybody have hints on it???
Thanks a lot
Luís
Reply to this comment
hola
hola
Reply to this comment
hola
hola
Reply to this comment
Thank you
please, send to me javascript function upload file.
Reply to this comment
Uploading a file through javaScript
How i can uploading a file through javaScript
Reply to this comment
Upload Dynamic file & check file size
Hello Frnds,
I make one script to upload a multiple file using javascript.It's not really upload a file at particular location just show me the file name.if i want to delete a file then also it'll work fine but when i upload a one file at that time i want to check file size also so i have option like ActiveXObject but ny application ll be run on linux platform so i'll not support.
So, Please Help me.
Thanks in advance
Gauttam
Reply to this comment
Its not working
Reply to this comment
please tell me gow it will work ....
Reply to this comment