html – 如何限制我的输入type =“file”仅接受在Firefox中不起作用的png图像文件

前端之家收集整理的这篇文章主要介绍了html – 如何限制我的输入type =“file”仅接受在Firefox中不起作用的png图像文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用input type =“file”,现在我的要求是我只想选择png图像,那就是当我选择浏览一个“png”过滤器应该被应用。

我已经介绍过www.w3schools.com/tags/att_input_accept.asp,下面是我正在使用的代码,Chrome可以正常工作,但不支持Firefox和IE。

请任何人帮我明白我一定在做什么错?

<h2>Below uses accept="image/*"</h2>
 <input type="file" name="pic1" accept="image/*" /> 

 <h2>Below I need to accept only for png</h2>
 <input type="file" name="pic1" accept="image/png" />

这是一个小提琴链接到它http://jsfiddle.net/Jcgja/2/

解决方法

您需要通过java脚本进行验证。以下是java脚本验证的代码
function CheckFileName() {
        var fileName = document.getElementById("uploadFile").value
        if (fileName == "") {
            alert("Browse to upload a valid File with png extension");
            return false;
        }
        else if (fileName.split(".")[1].toUpperCase() == "PNG")
            return true;
        else {
            alert("File with " + fileName.split(".")[1] + " is invalid. Upload a validfile with png extensions");
            return false;
        }
        return true;
    }
原文链接:https://www.f2er.com/html/233585.html

猜你在找的HTML相关文章