求大神给一个简略的js判断上传文件类型的代码
求大神给一个简单的js判断上传文件类型的代码
网上得感觉很难 求给一个html+js 判断上传类型只能是图片
------解决方案--------------------
截取文件后缀名。
------解决方案--------------------
<!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>无标题文档</title>
<script type="text/javascript">
function cc(m){
var t=m.value;
var type=t.lastIndexOf(".");
alert(t.substring(type+1));
}
</script>
</head>
<body>
<input type="file" onchange="cc(this)">
</body>
</html>
------解决方案--------------------
楼主看看成不。
网上得感觉很难 求给一个html+js 判断上传类型只能是图片
------解决方案--------------------
截取文件后缀名。
var str='xxx.png';
var d=/\.[^\.]+$/.exec(str);
alert(d);
------解决方案--------------------
<!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>无标题文档</title>
<script type="text/javascript">
function cc(m){
var t=m.value;
var type=t.lastIndexOf(".");
alert(t.substring(type+1));
}
</script>
</head>
<body>
<input type="file" onchange="cc(this)">
</body>
</html>
------解决方案--------------------
楼主看看成不。
<!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=utf-8" />
<title></title>
</head>
<body>
<input type="file" id="fileId" onchange="selectFile(this)" />
</body>
<script type="text/javascript">
function selectFile(input) {
var fileName = input.value;
if(fileName.length > 1 && fileName ) {
var ldot = fileName.lastIndexOf(".");
var type = fileName.substring(ldot + 1);
if(type != "img") {
alert(type);
//清除当前所选文件
input.outerHTML=input.outerHTML.replace(/(value=\").+\"/i,"$1\"");
}
}
}
</script>
</html>