参考:https://blog.csdn.net/wobaiwodedukuku/article/details/60571190
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form id="form1" name="form1" method="post" action="" enctype="multipart/form-data"> <!--<p><input type="hidden" name="MAX_FILE_SIZE" value="100000" /></p>--> <p><input name="userfile" id="userfile" type="file" οnchange="check()" /></p> </form> </body> </html> <script language="JavaScript" type="text/javascript"> function check() { var aa = document.getElementById("userfile").value.toLowerCase().split('.'); //以“.”分隔上传文件字符串 // var aa=document.form1.userfile.value.toLowerCase().split('.');//以“.”分隔上传文件字符串 if(document.form1.userfile.value == "") { alert('图片不能为空!'); return false; } else { if(aa[aa.length - 1] == 'gif' || aa[aa.length - 1] == 'jpg' || aa[aa.length - 1] == 'bmp' || aa[aa.length - 1] == 'png' || aa[aa.length - 1] == 'jpeg') //判断图片格式 { var imagSize = document.getElementById("userfile").files[0].size; alert("图片大小:" + imagSize + "B") if(imagSize < 1024 * 1024 * 3) alert("图片大小在3M以内,为:" + imagSize / (1024 * 1024) + "M"); return true; } else { alert('请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片'); //jpg和jpeg格式是一样的只是系统Windows认jpg,Mac OS认jpeg, //二者区别自行百度 return false; } } } </script>
转载于:https://www.cnblogs.com/ygyy/p/10484595.html