简体   繁体   中英

How to remove file upload control value in java script in java

I have file upload control for validating only(.xlsx|.xls) two extensions taking java script here. When i select valid extension it shows selected file name it's fine again. When i click browse button value is not clear. The value is clear when i select if invalid extension after raising the alert message click ok then only file upload control value is removing.

what i need when i select first valid file name after click again browse button value should be clear.

when i run this code in Mozilla fire fox it shows what i selected value. But in chrome it not showing invalid extension names.

My code:

<script>
    var extension = [".xlsx", ".xls"];
    function Validate(oInput) {
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < extension.length; j++) {
                    var sCurExtension = extension[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }
                if (!blnValid) {
                    alert("Sorry, invalid File, allowed extensions are: " + extension.join(", "));
                    oInput.value = "";
                    return false;
                }
            }
        }
        return true;
    }
</script>
Use this It's work.

<script type="text/javascript">
    //<![CDATA[
        function resetFileInput()
        {
            var parent = document.getElementById('fileInputContainer'),
                newFileInput = document.createElement('input');

            parent.removeChild(document.getElementById('file'));

            newFileInput.setAttribute('type', 'file');
            newFileInput.setAttribute('id', 'file');

            parent.appendChild(newFileInput);
        }
    //]]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM