仅上传特定格式文件的条件

a condition for upload file only with specific format

如何在用于上传图片的表单输入中定义一个条件,使用户只能上传特定格式的图片?

<div class="form-group">

    <div class="leftbox">
         <div class="col-sm-12" align="right">
              <input type="file" name="filedata" style="width:250px; background:none; " name="mipic"/>
                .jpeg,.jpg,.png
              </div>
         </div>
</div><br/>

您可以使用以下 JavaScript 函数来验证它。

function imageType() {
    var image = document.getElementById("Image").value;
    var extension;
    if (image == "") {
        alert("Upload a File With jpeg/jpg/png extension");
        return false;
    }
    else 
    {
        extension=image.split(".");
        if (extension[1] == "jpeg" ||extension[1] == "jpg" ||extension[1] == "png")
            return true;
        else {
            alert("File with " + image.split(".")[1] + " is not supported");
            return false;
        }
    }
    return true;
}
<input type="file" name="imagefilename" accept="image/x-png, image/gif, image/jpeg" />