How to restrict my input type=“file” to accept only png image files not working in Firefox

后端 未结 6 1694
终归单人心
终归单人心 2020-12-05 07:38

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.

I have

相关标签:
6条回答
  • 2020-12-05 07:56

    As you can see, the 'accept' attribute is not properly supported by any of the major browsers. You could use a javascript validation on the form onsubmit event to verify if the file type is correct, returning false otherwise.

    Do not use this attribute as a validation tool. File uploads should be validated on the server.

    0 讨论(0)
  • 2020-12-05 07:59

    you can use javascript function onChane event of a file function

    filesChange() {
            checkFile();
            } 
    <script type="text/javascript">
    function checkFile() {
        var fileElement = document.getElementById("uploadFile");
        var fileExtension = "";
        if (fileElement.value.lastIndexOf(".") > 0) {
            fileExtension = fileElement.value.substring(fileElement.value.lastIndexOf(".") + 1, fileElement.value.length);
        }
        if (fileExtension == "gif") {
            return true;
        }
        else {
            alert("You must select a GIF file for upload");
            return false;
        }
    }
    

    0 讨论(0)
  • 2020-12-05 08:01

    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;
        }
    
    0 讨论(0)
  • 2020-12-05 08:07
    <?php
    if ((($_FILES["pic1"]["type"] == "image/png")
    {
    if ($_FILES["pic1"]["error"] > 0)
    {
    echo "Error: " . $_FILES["pic1"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["pic1"]["name"] . "<br />";
    echo "Type: " . $_FILES["pic1"]["type"] . "<br />";
    echo "Size: " . ($_FILES["pic1"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["pic1"]["tmp_name"];
    }
    }
    else
    {
    echo "Invalid file";
    }
    ?>
    

    This is just the validation of the type of file. Not the whole upload script.

    0 讨论(0)
  • 2020-12-05 08:08

    As pointed out in the comments, accepted solution won't work with a file name with several "." in it. This should handle it better, it is more bug-proof and flexible, and you can manage accepted extensions by editing the array.

    function checkFileExtension() {
        var fileName = document.getElementById("uploadFile").value;
    
        if(!fileName)
          return false;
    
        var extension = fileName.split(".");
        if(extension && extension.length > 1){
            extension = [extension.length-1].toUpperCase();
            if (["PNG"].indexOf(extension) != -1)
                return true;
            else{
                alert("Browse to upload a valid File with png extension");
                return false;
            }
        }
        else{
            alert("Browse to upload a valid File with png extension");
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 08:09

    The browser support information on that page of w3schools is not correct.

    If you check this page, you see that the accept attribute it's not implemented in Firefox:

    https://developer.mozilla.org/en/HTML/Element/Input

    Update:
    The accept attribute is now implemented in Firefox, but users who don't have a recent version still won't have the support.

    0 讨论(0)
提交回复
热议问题