PDA

View Full Version : input-file element and javascript


jc70
08-24-2002, 04:10 PM
I don't like the look of the input-file element, so I've tried to put it
invisible with " style="display:none" " and simulate the onclick event with another button;
something like this:

<head>
<title>Form test</title>
<script language="JavaScript">
function file_changed() {
document.upload.filen.value = document.upload.fileu.value;
}
function browse_files() {
document.upload.fileu.click();
}
</script>
</head>
<body>
<form name="upload" method="post" action="upload.asp" enctype="multipart/form-data">
<input type="file" name="fileu" onchange="file_changed()">
<br>
<input type="text" name="filen" value="">
<br>
<button type="button" name="browse" onclick="browse_files()">Browse</button>
<br>
<button type="submit" name="post">Submit</button>
</form>
</body>
</html>

The problem is that when you try to submit the form he doesn't work;
The filename from the input-file element just disappear and nothinh happen unless
you press the submit button again, but in this case no filename is posted.

If you try to do it manually like this

<button type="button" name="post" onclick="document.upload.submit()">Submit</button>

instead of

<button type="submit" name="post">Submit</button>

you get this message: "Access denied"
if you use the Browse button of the input-file element everything works fine.

What is the problem, am I doing something wrong ???
Can someone help me, please ?

Thanks
Christian

Jon Hanlon
08-24-2002, 11:39 PM
All that the click() is doing is firing the onclick() event.
See http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/click.asp

The internal workings of the <input type=file> element aren't being triggered.

I don't think you'll be able to do what you want.

COBOLdinosaur
08-25-2002, 12:50 PM
>>>What is the problem, am I doing something wrong ???

Very simple. Your are violating built in security. You cannot upload a file with the user entering it the file text box or selecting it from the browse. It does not matter how you approach it, you are not permitted to modify the value of the input type file with scripting. If this was possible then a web site could take files from your computer without your permission.