JavaScript获取上传文件的文件名及扩展名

来源:互联网 发布:cms建站教程 编辑:程序博客网 时间:2024/06/05 21:28
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>获取上传文件的文件名及扩展名</title>
</head>
<body>
<script language="javascript">
<!--
function getFileName(obj)
{
    
var pos = obj.value.lastIndexOf("/")*1;
    
return obj.value.substring(pos+1);
}


function getFileExt(obj)
{
    
return obj.value.replace(/.+./,"");
}


function showInfo(obj)
{
    
var filename = "文件名:" + getFileName(obj);
    
var fileext = "扩展名:" + getFileExt(obj);
    document.getElementById(
"filename").innerText = filename;
    document.getElementById(
"fileext").innerText = fileext;
}

-->
</script>
<div id="filename" style="font-size:12px; "></div><br>
<div id="fileext" style="font-size:12px; "></div><br>
<input type="file" name="file1" onChange="showInfo(this);">
</body>
</html>
 
原创粉丝点击