PDA

View Full Version : Setting input type="file" value via Javascript


naesc76
09-27-2006, 09:00 PM
Basically I have a text input and a file input. I want to take the value of the text input and stick it in the file input. Simple HTML + Javascript is:

<input type="text" id="myText" />
<input type="file" id="myFile" />

<script type="text/javascript">
function doIt() {
var textInput = document.getElementById("myText");
var fileInput = document.getElementById("myFile");

fileInput.value = textInput.value;
}
</script>

This seems like a pretty simple thing to want to do but I guess it's a security no-no (?)... Firefox says:

"Exception: "security error" code : "0x805303e8 (NS_SECURITY_DOM_SECURITY_ERR)" location... line 56

Anyone know a way to get this to work?

_Aerospace_Eng_
09-27-2006, 11:47 PM
Read what its telling you.
"Exception: "security error" code : "0x805303e8 (NS_SECURITY_DOM_SECURITY_ERR)" location... line 56
Its a security issue. Because of how sensitive that field is you aren't allowed to set its value.

Basscyst
09-28-2006, 01:42 AM
Are you trying to make a custom file input? If so there are workarounds for that, but not in the way that you are attempting.

subodhpatil
03-23-2009, 11:44 AM
I'm afraid it is impossible to set a default value for file input. If you could, it would be a huge security risk because you can just upload any file on the user's computer on to yours without their notice even (with Javascript submit() and stuff).