javascript 選擇檔案夾

來源:互聯網
上載者:User

解決方案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控制項進行初始化和指令碼運行"----"啟用" 

browserFolder.js:
/**//***
    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);//起始目錄為:我的電腦
  //var Folder = 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>
                            <input type="text" name="path" />
                        </td>
                        <td>
                            <input type="button" onclick="browseFolder('path')"
                                value="選擇產生路徑" />
                        </td>
2.解決方案二:
 自己寫一個js讀取本地硬碟的選擇框, 缺點是外觀上較上一個差一些. <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</head>
<body>
<table border="0" cellpadding="0" width="100%" id="tb_show">       
        <tr>
            <td width="18%">檔案儲存位置:</td>
            <td width="82%">
                <%--<html:file property="file" size="40"  styleClass="inputbox"/>--%>
                <input name="backDir" type="text" value ="C:/" size="100" width="500">
            </td>
        </tr>
       
        <tr>
            <td>目錄位置:</td>
            <td>
                <select name="tables_drive" id="tables_drives" onchange="get_drives()" ></select>
            </td>
        </tr>
       
        <tr>   
            <td colspan="2">           
                <select name="table_folder" id="table_folder"  size="10" multiple ondblclick="get_file()"></select>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <font color="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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.