(Transfer) JS custom function to judge the type and size of the files uploaded by the web front-end

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>web前端对上传的文件进行类型大小判断的js自定义函数</title>
</head>
<body>
   
    <form>
        <input type="file" id="fileId">
        <input type="button" value = "提交" onclick="checkFileSizeAndType(10*1024*1024,['gif','jpg','png'],'fileId');">
    </form>
    <script>     the id of the input type=file box     //allowType represents the file type (suffix) allowed to be uploaded //fileId represents     //maxSize represents the maximum file size allowed to be uploaded, in bytes
    //Parameter description



    function checkFileSizeAndType(maxSize,allowType,fileId) {
                //default size
                if(!maxSize){
                    maxSize=10*1024*1024;
                }
                //default type
                if(!allowType){
                    allowType=new Array('jpg','png ');   
                }
                //js gets the uploaded file object by id
                var file = document.getElementById(fileId);           
                var types =allowType;
               
                var fileInfo = file.files[0];
                if(!fileInfo){
                    alert("Please choose file!");
                    return false;
                }
                var fileName = fileInfo.name;
               
                //Get the file suffix
                var file_typename = fileName.substring(
                        fileName.lastIndexOf('.') + 1, fileName.length);
               
                //Define whether the flag can be submitted for upload
                var isUpload = true;
                / /Define an error parameter: 1 means the size exceeds 2 means the type does not support
                var errNum =0;
                if (fileInfo) {
                    if (fileInfo.size > maxSize) {
                        isUpload = false;
                        errNum=1;
                    } else {
                        for ( var i in types) {
                            if (types[i] == file_typename) {
                                isUpload = true;
                                return isUpload;
                            } else {
                                isUpload = false;
                                errNum=2;
                            }
                        }

                    }

                }
                //Prompt for the wrong type
                if (!isUpload) {
                    if(errNum==1){
                        var size = maxSize /1024/1024;
                        alert("The uploaded file must be an image smaller than "+size+"M!");
                    }else if(errNum==2){
                        alert("Uploaded "+file_typename+" file type is not supported! Only "+types.toString()+" format");
                    }else{
                        alert("No file selected");
                    }
                    file.value="";
                    return isUpload;
               
                   
                }
            }

   
    </script>
</body>
</html>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326721292&siteId=291194637