file field value

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

file field value

Post by m2babaey »

Hi
I use this code in my ajax action file to clear form field after submit:

Code: Select all

<script>
parent.document.getElementById('field_id').value='';
</script>
It works for input fields but doesn't work for file fields in IE
Any help would be extremely appreciated
Thanks in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: file field value

Post by Christopher »

You might want to try element.setAttribute("value", "").
(#10850)
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Re: file field value

Post by m2babaey »

Thanks
But I have not used this method before
Should I replace:

Code: Select all

<script>
parent.document.getElementById('field_id').value='';
</script>
with

Code: Select all

<script>
field_id.setAttribute("value", "").
</script>
?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: file field value

Post by John Cartwright »

If you are accessing the parent's DOM, then

[text]parent.document.getElementById('field_id').setAttribute('value', '');[/text]
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: file field value

Post by ell0bo »

You can't force the value of a file input type, doing so would be a security issue.

Think of it this way, I create a page that has a hidden form. I keep rotating the value of that input field to look for files I might want to steal. I then auto submit the page to an iframe. I would be able to just steal files off of anyone's computer.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: file field value

Post by John Cartwright »

ell0bo wrote:You can't force the value of a file input type, doing so would be a security issue.

Think of it this way, I create a page that has a hidden form. I keep rotating the value of that input field to look for files I might want to steal. I then auto submit the page to an iframe. I would be able to just steal files off of anyone's computer.
Excellent point.

After discussing this with a colleague, evidently a good way to manipulate the file input to it's original blank value is to re-insert the file input back into the DOM, thus overwriting the current input element. I.e.,

Code: Select all

<script>
    function clearFileInput(elementId)
    {
        document.getElementById(elementId).innerHTML = document.getElementById(elementId).innerHTML;
    }
</script>

<div id="fileInputContainer">
    <input type="file" name="myFileUpload" /> 
</div> 

<a href="#" onclick="clearFileInput('fileInputContainer');">Clear File Input</a>
Alternatively, you can use JS to change the input type of the HTML, then change it back to a file input. This will clear the currently set value. I.e.,

Code: Select all

<script>   
    function clearFileInput(elementId) {
        document.getElementById(elementId).setAttribute('type', 'input');
        document.getElementById(elementId).setAttribute('type', 'file'); 
    }
</script>


<input type="file" name="myFileUpload" id="fileInput" /> 

<a href="#" onclick="clearFileInput('fileInput');">Clear File Input</a>
Post Reply