Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
heyy..

I am using a jquery for validation of images ..My problem when alert msg is came and click OK button ..the fileupload is not cleared(in IE) ..But in mozila it cleared..here is my code.

XML
<script type="text/javascript">
    $(function() {
        $('#<%=fluPhoto.ClientID %>').change(
    function() {
        var a = $('#<%=fluPhoto.ClientID %>').val();
        //alert(a);
        var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
        if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
            $("input[attr-identity=fluPhoto]").val('');
            MessageBox("Only .jpeg, .jpg, .png, .gif, .bmp formats are allowed.", $('#<%=fluPhoto.ClientID %>'));


        }


    })
    })



</script>



can I get solution for clear the file upload(fluPhoto) after the validation occure..
Posted
Comments
I.explore.code 23-Apr-13 6:19am    
Not sure about this, but can you try this: $("input[attr-identity=fluPhoto]").attr('value', '');

The file upload control is programmatically read-only in IE, for security purposes. The only option is to reset the whole form.

The best solution I found is explained here:
http://stackoverflow.com/questions/1043957/clearing-input-type-file-using-jquery[^]
See slipheed's solution, which is to temporarily wrap the control in its own form to reset it, then remove the wrapper.
 
Share this answer
 
Hello,

File Upload element is a protected element in most of the browsers and browser's will not allow Javascript to alter the value of this element[^].

However you can delete this element on the fly and re-create the same with exact same attributes of the deleted element and append it in the DOM, this way it will clear the value of this element. below code snippet shows how this canbe done.
HTML
lt;head>

<script type="text/javascript">
function clearForm(ctrlId) {
   var old = document.getElementById(ctrlId);
   var newElm = document.createElement('input');
   newElm.type = "file";
   newElm.id = ctrlId;
   newElm.name = old.name;
   newElm.className = old.className;
   // Put code to copy other attributes as well
   old.parentNode.replaceChild(newElm, old);
}
</script>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<form id="frmMain">
    <div id="fuDiv">
         <input type="file" id="fluPhoto"/><br/>
    </div>
    <a href="#" onclick="clearForm('fluPhoto')">Clear</a>
</form>
</body>
</html>

Regards,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900