Js 获取本地文件路径

<html>

<head>

<script>

/**
* 获取本地驱动器信息
* @return Array 驱动器信息的列表
*/

var fso = new ActiveXObject("Scripting.FileSystemObject");

function getDriveInfo()
{

    //创建FSO实例
 document.getElementById("curPath").value = "";
    var Enum = new Enumerator(fso.Drives);
   //驱动器信息列表
    var driveInfoList=new Array();
    var s="";
    for(Enum.moveFirst(); !Enum.atEnd(); Enum.moveNext())
    {
      //遍历获取驱动器信息
       var drive = Enum.item();
    
       var driveTypeDesc="";
     //获取驱动器信息的类型
      if(drive.DriveType==2)
      {
         driveTypeDesc="本地磁盘";
      }
      else if(drive.DriveType==1)
      {
        driveTypeDesc="可移动磁盘";
      }
      else if(drive.DriveType==4)
      {
        driveTypeDesc="光驱";
      }
   try{
  var obj=new Object();
        obj.value=drive.DriveLetter;
        obj.name=drive.VolumeName+"("+drive.DriveLetter+":)"+driveTypeDesc;
  s += "<a href='#' onclick='javascript:selectFolder(this)'>" + obj.value + ":// </a>" ;
  s += "<br>";
   }catch(err){
  
   }
     
      driveInfoList.push(obj);
   }
   document.getElementById("folder").innerHTML = s;
    return driveInfoList;
}

function selectFolder(obj,str){
 var folder = obj.innerHTML;
 ShowFolderList1(folder);
}

function selectFiles(obj){
 alert(obj.value);
}

function jump(){
 var curPath = document.getElementById("curPath").value;
 try{
  if(curPath == ""){
   getDriveInfo();
  }else{
   ShowFolderList1(curPath);
  }
 }catch(err){
  document.getElementById("folder").innerHTML = "找不到路径:" + document.getElementById("curPath").value;
 }
}

function jumpParent(){
 var curPath = document.getElementById("curPath").value;
 try{
  if(curPath == ""){
   getDriveInfo();
  }else{
   var s = curPath.substring(curPath.lastIndexOf("//"),curPath.length);
   if(s==null || s==""){
    getDriveInfo();
   }else{
    ShowFolderList1(curPath.replace(s,""));
   }
  }
 }catch(err){
  getDriveInfo();
 }
}

</script>

</head>

<body onload="getDriveInfo()">
 <table align="center" width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
  <tr bgcolor="#FFFFFF" align="left">
   <td align="center" align="left">地址&nbsp;&nbsp;<input type="text" size="60" name="curPath" class="textbox" value="" id="curPath"/>
             <input type="button" value="转到" class="button" onclick="jump()"/> <input type="button" value="上级目录" onclick="jumpParent()"></td>
  </tr>
 </table>
 <div id="folder"></div>
</body>
<SCRIPT>
function ShowFolderList1(folderspec)
{
document.getElementById("curPath").value=folderspec;
var fso, f, fc, s;
s = "";
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(folderspec);

fc = new Enumerator(f.SubFolders); 
for (; !fc.atEnd(); fc.moveNext())
{
s += "<input type='checkbox' name='filesDelete' value=" + fc.item() + "onclick='selectFiles(this)' />/n";
s += "<a href='#' onclick='javascript:selectFolder(this,2)'>" + fc.item() + "</a>";
s += "<br>";
}

fc = new Enumerator(f.Files);
for (; !fc.atEnd(); fc.moveNext())
{
s += "<input type='checkbox' name='filesDelete' value=" + fc.item() + "onclick='selectFiles(this)' />/n";
s += fc.item();
s += "<br>";
}

document.getElementById("folder").innerHTML = s;
}
</SCRIPT>
</html>

你可能感兴趣的:(JavaScript,function,Class,input,button,textbox)