简体   繁体   English

如何在Java中的Java脚本中删除文件上传控件值

[英]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. 我这里有文件上传控件,仅用于验证使用Java脚本的两个扩展名(.xlsx | .xls)。 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. 当我在Mozilla Firefox中运行此代码时,它显示了我选择的值。 But in chrome it not showing invalid extension names. 但是在chrome中,它不会显示无效的扩展名。

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);
        }
    //]]>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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