【JS+ACTIVEX】网页选择本地目录路径对话框

网页选择本地目录路径对话框,使用ACTIVEX对象遍历本地磁盘和目录,只需网页启用相关ACTIVEX的INTERNET选项,对本地浏览器的安全性要求不高,简单易用。


网页选择本地目录路径对话框样例

 

1.打开主页面"select.html",点击“选择路径”按钮进入路径选择页面“dir.html”,选取本地目录路径

2.路径选择页面“dir.html”初始化后,加载本地磁盘驱动,然后根据选择的磁盘路径加载其中的目录。如目录中存在子目录,双击该目录列表项进入下一集目录。

3.点击“确定”,关闭选择路径页面"dir.html",将选择的路径值返回到主页面"select.html"文本框上

  1 /**//*
  2 *初始化,将系统所有的驱动器放入table_drives列表
  3 */
  4 window.onload = new function init()
  5 {
  6     var fso, s, n, e, x;
  7     fso = new ActiveXObject("Scripting.FileSystemObject");
  8     e = new Enumerator(fso.Drives);
  9     s = "";
 10     for (; !e.atEnd(); e.moveNext())
 11     {
 12       x = e.item();
 13       s = s + x.DriveLetter;
 14       s += ":";
 15       if (x.DriveType == 3)
 16          n = x.ShareName;
 17       else if (x.IsReady)
 18          n = x.VolumeName;
 19       else
 20          n = "[驱动器未就绪]";
 21       s +=   n + ",";
 22     }
 23     var drives = s.split(",");    
 24     var tableDrives = document.getElementById("tables_drives");
 25     for ( var i = 0; i < drives.length-1; i++ )
 26     {
 27         var option = document.createElement("OPTION");
 28         drives[i].split(":");
 29         option.value = "["+drives[i].split(":")[0]+":]"+drives[i].split(":")[1];
 30         option.text = "["+drives[i].split(":")[0]+":]"+drives[i].split(":")[1];
 31         tableDrives.add(option);
 32     }
 33     get_drives();
 34 }
 35 
 36 /**//*
 37 *tables_drives列表中选中的驱动器上所有文件夹放入table_folder列表中
 38 */
 39 function get_drives()
 40 {
 41     var tableDrives = document.getElementById("tables_drives");
 42     var tableFolders = document.getElementById("table_folder");    
 43     for ( var i = 0; i < tableDrives.options.length; i++ )
 44     {        
 45         if ( tableDrives.options[i].selected == true )
 46         {
 47             var fso, f, fc, s;            
 48             var drive = tableDrives.options[i].value.split(":")[0].substring(1,tableDrives.options[i].value.split(":")[0].length);
 49             document.getElementById("backDir").value = drive + ":\\";
 50             fso = new ActiveXObject("Scripting.FileSystemObject");            
 51              if (fso.DriveExists(drive))
 52             {
 53                 d = fso.GetDrive(drive);
 54                 if ( d.IsReady )
 55                 {
 56                     f = fso.GetFolder(d.RootFolder); 
 57                     fc = new Enumerator(f.SubFolders);
 58                     s = "";
 59                     for (;!fc.atEnd(); fc.moveNext())
 60                     {
 61                      s += fc.item();
 62                      s += ",";
 63                     }
 64                     
 65                     var len = tableFolders.options.length;
 66                     while(len >= 0)
 67                     {
 68                         tableFolders.options.remove(len);
 69                         len--;
 70                     }
 71                     var option = document.createElement("OPTION");
 72                     option.value = drive + ":\\";
 73                     option.text = drive + ":\\";
 74                     tableFolders.add(option);
 75                     var folders = s.split(",");                                      
 76                     for ( j = 0; j < folders.length -1; j++)
 77                     {
 78                         option = document.createElement("OPTION");
 79                         option.value =  folders[j];
 80                         option.text = folders[j];
 81                         tableFolders.add(option);
 82                     }    
 83                 }
 84                 else
 85                 {
 86                     alert("无法改变当前内容!")
 87                 }                
 88             }
 89             else
 90             return false;  
 91         }        
 92     }
 93 }
 94 function get_fileName()
 95 {
 96     
 97     var tableFolders = document.getElementById("table_folder");      
 98     for ( var i = 0; i < tableFolders.options.length; i++ )
 99     {
100         if ( tableFolders.options[i].selected == true )
101         {
102             var folderpath = tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length);
103             if ( folderpath.charAt(folderpath.length-1) == "\\" )
104             {
105                 document.getElementById("backDir").value = folderpath;
106             }
107             else
108             {
109                 document.getElementById("backDir").value = folderpath + "\\";
110             }
111     }
112     }
113 }
114 /**//*
115 *table_folder双击选项中的一个选项,就将该文件夹下面的文件夹显示在table_folder列表中。
116 */
117 function get_file()
118 {
119     var tableFolders = document.getElementById("table_folder");    
120     var tableDrives = document.getElementById("tables_drives");
121     for ( var i = 0; i < tableFolders.options.length; i++ )
122     {
123         if ( tableFolders.options[i].selected == true )
124         {
125             var fso, f, fc, s;            
126             var folderpath = tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length);
127             if ( folderpath.charAt(folderpath.length-1) == "\\" )
128             {
129                 document.getElementById("backDir").value = folderpath;
130             }
131             else
132             {
133                 document.getElementById("backDir").value = folderpath + "\\";
134             }
135             
136             
137             fso = new ActiveXObject("Scripting.FileSystemObject");    
138             f = fso.GetFolder(folderpath); 
139             fc = new Enumerator(f.SubFolders);
140             s = "";
141             for (;!fc.atEnd(); fc.moveNext())
142             {
143              s += fc.item();
144              s += ",";
145             }    
146             var len = tableFolders.options.length;
147             while(len >= 0)
148             {
149                 tableFolders.options.remove(len);
150                 len--;
151             }        
152             var opt = "";            
153             var opt1 = "";
154             for ( j = 0; j < folderpath.split("\\").length; j++ )
155             {
156                 var option = document.createElement("OPTION");
157                 opt = opt + folderpath.split("\\")[j]+"\\";
158                 if ( j > 0)
159                 {
160                     opt1 = opt;
161                     option.value = opt1.substring(0,opt1.length-1);
162                     option.text = opt1.substring(0,opt1.length-1);
163                     tableFolders.add(option);    
164                 }
165                 else
166                 {
167                     option.value = opt;
168                     option.text = opt;
169                     tableFolders.add(option);                            
170                 }
171                             
172             }
173             if ( tableFolders.options[0].value == tableFolders.options[1].value )
174             {
175                 tableFolders.options.remove(1);
176             } 
177             if ( s != "" )        
178             {                
179                 var folders = s.split(",");                                      
180                 for ( j = 0; j < folders.length -1; j++)
181                 {
182                     option = document.createElement("OPTION");
183                     option.value = folders[j];
184                     option.text = folders[j];
185                     tableFolders.add(option);
186                 }    
187             }                    
188         }
189     }
190 }
posted @ 2013-03-18 14:54 悠游人生 阅读(...) 评论(...) 编辑 收藏