Click to See Complete Forum and Search --> : Clear File Input Box
Andy_ADS
12-11-2003, 03:52 PM
I am validating the contentes of a File Input Box for filename length. I am using .value="" to clear the value and that works programatically to eliminate the upload, but the Input Box is still filled with the "old" information.
Is there a way to clear this Input Box so users are not confused.
Thanks and here is my code
function ValUpload(inEle){
var up_name = inEle.name;
var Value =inEle.value;
var up_array = Value.split("\\");
var num = up_array.length;
if(up_array[num-1].length > 30){
alert("Some Message");
inEle.value = "";
}//end if
}//end sub
Andy
fredmv
12-11-2003, 05:25 PM
You can't change the value of a file upload form control for security reasons (uploading a password or other private file for example). However, you could always reset the form resulting the the value of the element being restored to it's default value of nothing:<input type="reset" />
freeozy
06-20-2008, 05:08 AM
there is still one way to clear the input file without reset. innerHTML
the code below also checks the value of file input if it's an image or not. mimetype checking (image control) can be also made after submit the form to improve security.
accept='image/jpg' is not working in iexplorer.
function checkImg(val){
var dgr = val.value;
dgr = dgr.substr(dgr.length-4, dgr.length)
dgr = dgr.toLowerCase();
if (dgr =='.jpg' || dgr == 'jpeg' || dgr== '.gif'){
alert('image');
}else{
alert('not image');
var objD = document.forms['form1'].divR;
objD.innerHTML = '';
objD.innerHTML = '<input id="uImg" name="uImg" type="file" class="defText" style="font-size: 10pt" onchange="javascript: checkImg(this);" size="30">';
}
}
<form name="form1" id="form1" method="post">
<div id="divR" name="divR">
<input id="uImg" name="uImg" type="file" class="defText" style="font-size: 10pt" onchange="javascript: checkImg(this);" size="30">
</div>
</form>
it works fine for me
snagler
03-04-2009, 08:36 PM
Hi all,
Essentially, the input file field is "read-only" and you can't change it. However,
you can re-establish it in the DOM which will force the browser to reset its value to nothing. Here's how.
Your simple input file form:
<form name="form1" method="post">
<div>
<input id="inputField" type="file">
</div>
</form>
JavaScript to reset the file field:
document.getElementById('inputField').setAttribute('type', 'input');
document.getElementById('inputField').setAttribute('type', 'file');
I run this code on every page load so that it clears the file field whether its from pressing the browser refresh button, backing into the page or via link.
window.onload = function(){
document.getElementById('inputField').setAttribute('type', 'input');
document.getElementById('inputField').setAttribute('type', 'file');
}
agresso
05-03-2009, 12:31 PM
Here is a good description:
http://gusiev.com/?p=11