0

I have jQuery function to validate the file extension of the uploaded file. If the the file does not belong to a desired file extensions then a validation message will be shown and I will clear the value of File Upload via jQuery as

$('#ctl00_ContentPlaceHolder1_FileUpload').val("")

It is working fine in modern browsers but not in IE 8. I also tried

document.getElementById('ctl00_ContentPlaceHolder1_FileUpload').value = ''

but not working also. my full code is

if ($.inArray(ext, ['gif', 'png', 'jpg', 'jpeg', 'bmp']) == -1) {
    alert('Image must be .jpg/.jpeg/.gif/.bmp/.png only.');
    $('#ctl00_ContentPlaceHolder1_FileUpload').val("");
    document.getElementById('ctl00_ContentPlaceHolder1_FileUpload').value = '';
    $("#ctl00_ContentPlaceHolder1_FileUpload").focus();
}

1 Answer 1

0

IE has some security restriction(read-only) over <input type="file">

So, a workaround would be clone() and replace the current <input>:

if(navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0){
    $("input[type='file']").replaceWith($("input[type='file']").clone(true));
} else {
    $("input[type='file']").val('');
}

Also, form $('#form')[0].reset(); can be an option in case it is the only field in the form.

Sign up to request clarification or add additional context in comments.

3 Comments

When I try to Implement the above code I am getting "'$.browser.msie' is null or not an object" error
what is the jquery version you are using? $.browser.msie is deprecated now.
Thanks @shaunakde The .browser call has been removed in jquery 1.9 so I referenced jquery-migrate-1.2.1.js on this Link in my page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.