如何限制我的输入类型=“文件"只接受在 Firefox 中不起作用的 png 图像文件 [英] How to restrict my input type="file" to accept only png image files not working in Firefox

查看:16
本文介绍了如何限制我的输入类型=“文件"只接受在 Firefox 中不起作用的 png 图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 input type="file",现在我的要求是我只想选择 png 图片,也就是说,当我选择浏览时,应该应用png"过滤器.

I am using input type="file", Now my requirement is that I only want to select png images, that is when I select browse a "png" filter should be applied.

我已经提到了 www.w3schools.com/tags/att_input_accept.asp,下面是我正在使用的代码,这适用于 Chrome,但不适用于 Firefox 和 IE.

I have referred www.w3schools.com/tags/att_input_accept.asp and below is the code I am using, this works fine with Chrome but does not work with Firefox and IE.

请谁能帮我理解我一定做错了什么?

Please can anyone help me understand what wrong I must be doing ?

 <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脚本验证的代码

You need to validate it through java script. Below is the code for java script validation

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;
    }

这篇关于如何限制我的输入类型=“文件"只接受在 Firefox 中不起作用的 png 图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆