Forums | MacLife
You are not logged in.
#1 2009-02-01 1:50 am
Why doesn't this work ?? removing file input node
For security reasons, the value of file input nodes are read only.
Thus is a validation script tries to validate a file input and it fails, it seems you can not reset it without resetting the entire form.
So I tried removing the node and inserting a new one:
Code:
function validateImage(image,voucher) {
var myInput = document.getElementById(image).value
if (emptyInput(myInput)) {
return true;
} else {
if (validFileType(myInput)) {
return true;
} else {
var myDiv = document.getElementById(voucher);
myDiv.removeChild(myDiv.firstChild);
var myImage = document.createElement("input");
myImage.setAttribute("type","file");
myImage.setAttribute("name","voucher[]");
myImage.setAttribute("style","width: 42em;");
myImage.setAttribute("id",image);
myDiv.appendChild(myImage);
return false;
}
}
}Both opera and firefox fail to remove the node, but are more than willing to insert the replacement - which causes a node id clash that pukes opera (but firefox seems to handle, interestingly w/o errors to the console).
Does the read only status of file inputs prevent a note with a selected file from being deleted, or is my code wrong?
It seems that removing a node should not cause any security concerns.
Is there any proper way to reset a file input without resetting the entire form?
It would seem to me that if resetting the entire form is not a security issue, then resetting a file input node to blank shouldn't be either.
There are two kinds of people who keep rattlesnakes.
Those who have been bit, and those who will be bit. - Al Wolf.
Offline
#2 2009-02-01 3:05 am
Re: Why doesn't this work ?? removing file input node
I'm starting to make stupid mistakes so I'm hitting the hay, but I think maybe I need to try remove all the children of the div that has the input - maybe it's picking up the carriage return as a text node or something. I'll try that tomorrow.
If that doesn't work, then I won't have my js validation script exit the form submit on wrong image types (I check everything server side anyway and thus give a chance to fix it w/o needing to refill all the other data if it is not an accepted filetype according to fileinfo)
There are two kinds of people who keep rattlesnakes.
Those who have been bit, and those who will be bit. - Al Wolf.
Offline
#3 2009-02-01 6:24 am
Re: Why doesn't this work ?? removing file input node
That was it - kill all the children and it does what I want to do (thus preventing un-necessary file uploads)
There are two kinds of people who keep rattlesnakes.
Those who have been bit, and those who will be bit. - Al Wolf.
Offline

