解決方案1:
調用windows的shell,但會有安全問題.
* browseFolder.js
* 該文件定義了BrowseFolder()函數,它將提供一個文件夾選擇對話框
* 以供用戶實現對系統文件夾選擇的功能
* 文件夾選擇對話框起始目錄由
* Shell.BrowseForFolder(WINDOW_HANDLE, Message, OPTIONS, strPath)函數
* 的strPath參數設置
* 例如:0x11--我的電腦
* 0 --桌面
* "c:\\"--系統C盤
*
* 用如下代碼把該函數應用到一個HTML文件中:
* <script src="browseFolder.js"></script>
* 或把下面代碼直接COPY到<script language="javascript">...</script>標簽中;
* 特別注意的是,由于安全方面的問題,你還需要如下設置才能使本JS代碼正確運行,
* 否者會出現"沒有權限"的問題.
*
* 1、設置可信任站點(例如本地的可以為:http://localhost)
* 2、其次:可信任站點安全級別自定義設置中:設置下面的選項
* "對沒有標記為安全的ActiveX控件進行初始化和腳本運行"----"啟用"
- /***
- path要顯示值的對象id
- ****/
- function browseFolder(path){
- try {
- var Message= "\u8bf7\u9009\u62e9\u6587\u4ef6\u5939" ; //選擇框提示信息
- var Shell= new ActiveXObject( "Shell.Application" );
- var Folder=Shell.BrowseForFolder(0,Message,64,17); //起始目錄為:我的電腦
- //varFolder=Shell.BrowseForFolder(0,Message,0);//起始目錄為:桌面
- if (Folder!= null ){
- Folder=Folder.items(); //返回FolderItems對象
- Folder=Folder.item(); //返回Folderitem對象
- Folder=Folder.Path; //返回路徑
- if (Folder.charAt(Folder.length-1)!= "\\" ){
- Folder=Folder+ "\\" ;
- }
- document.getElementById(path).value=Folder;
- return Folder;
- }
- }
- catch (e){
- alert(e.message);
- }
- }
使用的時候:
- <td>
- <inputtype= "text" name= "path" />
- </td>
- <td>
- <inputtype= "button" onclick= "browseFolder('path')"
- value= "選擇生成路徑" />
- </td>
2.解決方案二:
自己寫一個js讀取本地硬盤的選擇框, 缺點是外觀上較上一個差一些.
- <html>
- <head>
- <metahttp-equiv= "Content-Type" content= "text/html;charset=gb2312" >
- <title>無標題文檔</title>
- </head>
- <body>
- <tableborder= "0" cellpadding= "0" width= "100%" id= "tb_show" >
- <tr>
- <tdwidth= "18%" >文件保存位置:</td>
- <tdwidth= "82%" >
- <%--<html:fileproperty= "file" size= "40" styleClass= "inputbox" />--%>
- <inputname= "backDir" type= "text" value= "C:\"size=" 100 "width=" 500">
- </td>
- </tr>
- <tr>
- <td>目錄位置:</td>
- <td>
- <selectname= "tables_drive" id= "tables_drives" onchange= "get_drives()" ></select>
- </td>
- </tr>
- <tr>
- <tdcolspan= "2" >
- <selectname= "table_folder" id= "table_folder" size= "10" multipleondblclick= "get_file()" ></select>
- </td>
- </tr>
- <tr>
- <tdcolspan= "2" >
- <fontcolor= "red" >說明:雙擊列表框的一個選項,就將該文件夾下面的文件夾顯示在該列表框中。第一個就是根目錄</font>
- </td>
- </tr>
- </table>
- </body>
- </html>
- <script>
- /**/ /*
- *初始化,將系統所有的驅動器放入table_drives列表
- */
- window.onload= new function init()
- {
- var fso,s,n,e,x;
- fso= new ActiveXObject( "Scripting.FileSystemObject" );
- e= new Enumerator(fso.Drives);
- s= "" ;
- for (;!e.atEnd();e.moveNext())
- {
- x=e.item();
- s=s+x.DriveLetter;
- s+= ":" ;
- if (x.DriveType==3)
- n=x.ShareName;
- else if (x.IsReady)
- n=x.VolumeName;
- else
- n= "[驅動器未就緒]" ;
- s+=n+ "," ;
- }
- var drives=s.split( "," );
- var tableDrives=document.getElementById( "tables_drives" );
- for ( var i=0;i<drives.length-1;i++)
- {
- var option=document.createElement( "OPTION" );
- drives[i].split( ":" );
- option.value= "[" +drives[i].split( ":" )[0]+ ":]" +drives[i].split( ":" )[1];
- option.text= "[" +drives[i].split( ":" )[0]+ ":]" +drives[i].split( ":" )[1];
- tableDrives.add(option);
- }
- }
- /**/ /*
- *tables_drives列表中選中的驅動器上所有文件夾放入table_folder列表中
- */
- function get_drives()
- {
- var tableDrives=document.getElementById( "tables_drives" );
- var tableFolders=document.getElementById( "table_folder" );
- for ( var i=0;i<tableDrives.options.length;i++)
- {
- if (tableDrives.options[i].selected== true )
- {
- var fso,f,fc,s;
- var drive=tableDrives.options[i].value.split( ":" )[0].substring(1,tableDrives.options[i].value.split( ":" )[0].length);
- document.getElementById( "backDir" ).value=drive+ ":\\" ;
- fso= new ActiveXObject( "Scripting.FileSystemObject" );
- if (fso.DriveExists(drive))
- {
- d=fso.GetDrive(drive);
- if (d.IsReady)
- {
- f=fso.GetFolder(d.RootFolder);
- fc= new Enumerator(f.SubFolders);
- s= "" ;
- for (;!fc.atEnd();fc.moveNext())
- {
- s+=fc.item();
- s+= "," ;
- }
- var len=tableFolders.options.length;
- while (len>=0)
- {
- tableFolders.options.remove(len);
- len--;
- }
- var option=document.createElement( "OPTION" );
- option.value=drive+ ":\\" ;
- option.text=drive+ ":\\" ;
- tableFolders.add(option);
- var folders=s.split( "," );
- for (j=0;j<folders.length-1;j++)
- {
- option=document.createElement( "OPTION" );
- option.value=folders[j];
- option.text=folders[j];
- tableFolders.add(option);
- }
- }
- else
- {
- alert( "無法改變當前內容!" )
- }
- }
- else
- return false ;
- }
- }
- }
- /**/ /*
- *table_folder雙擊選項中的一個選項,就將該文件夾下面的文件夾顯示在table_folder列表中。
- */
- function get_file()
- {
- var tableFolders=document.getElementById( "table_folder" );
- var tableDrives=document.getElementById( "tables_drives" );
- for ( var i=0;i<tableFolders.options.length;i++)
- {
- if (tableFolders.options[i].selected== true )
- {
- var fso,f,fc,s;
- var folderpath=tableFolders.options[i].value.substring(0,tableFolders.options[i].value.length);
- if (folderpath.charAt(folderpath.length-1)== "\\" )
- {
- document.getElementById( "backDir" ).value=folderpath;
- }
- else
- {
- document.getElementById( "backDir" ).value=folderpath+ "\\" ;
- }
- fso= new ActiveXObject( "Scripting.FileSystemObject" );
- f=fso.GetFolder(folderpath);
- fc= new Enumerator(f.SubFolders);
- s= "" ;
- for (;!fc.atEnd();fc.moveNext())
- {
- s+=fc.item();
- s+= "," ;
- }
- var len=tableFolders.options.length;
- while (len>=0)
- {
- tableFolders.options.remove(len);
- len--;
- }
- var opt= "" ;
- var opt1= "" ;
- for (j=0;j<folderpath.split( "\\" ).length;j++)
- {
- var option=document.createElement( "OPTION" );
- opt=opt+folderpath.split( "\\")[j]+" \\";
- if (j>0)
- {
- opt1=opt;
- option.value=opt1.substring(0,opt1.length-1);
- option.text=opt1.substring(0,opt1.length-1);
- tableFolders.add(option);
- }
- else
- {
- option.value=opt;
- option.text=opt;
- tableFolders.add(option);
- }
- }
- if (tableFolders.options[0].value==tableFolders.options[1].value)
- {
- tableFolders.options.remove(1);
- }
- if (s!= "" )
- {
- var folders=s.split( "," );
- for (j=0;j<folders.length-1;j++)
- {
- option=document.createElement( "OPTION" );
- option.value=folders[j];
- option.text=folders[j];
- tableFolders.add(option);
- }
- }
- }
- }
- }
-
</script>
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
