Source: http://www.blogjava.net/supercrsky/archive/2008/06/17/208641.html
Solution 1:
Windows Shell is called, but security issues may occur.
* Browsefolder. js
* This file defines the browsefolder () function, which provides a folder selection dialog box.
* Allows you to select system folders.
* The start directory of the folder selection dialog box is
* Shell. browseforfolder (window_handle, message, options, strpath) Function
* Strpath parameter settings
* Example: 0x11 -- my computer
* 0 -- Desktop
* "C:" -- System Drive C
*
* Use the followingCodeApply this function to an HTML file:
* <SCRIPT src = "browsefolder. js"> </SCRIPT>
* Copy the following code directly to the <script language = "JavaScript">... </SCRIPT> tag;
* Note that due to security issues, you still need to set the following settings to make the JS Code run correctly,
* Otherwise, "no permission" may occur.
*
* 1. Set a trusted site (for example, a local site can be http: // localhost)
* 2. Second, set the following options in the custom settings for trusted site security level:
* "Initialize ActiveX controls that are not marked as secure and run scripts" ---- "enable"
Browserfolder. JS:
/**//***
Object ID of the value to be displayed in Path
****/
Function browsefolder (PATH ){
Try {
VaR message = "\ u8bf7 \ u9009 \ u62e9 \ u6587 \ u4ef6 \ u5939"; // a message is displayed in the selection box.
VaR shell = new activexobject ("Shell. Application ");
VaR folder = shell. browseforfolder (0, message, 64, 17); // start Directory: My computer
// Var folder = shell. browseforfolder (0, message, 0); // start Directory: Desktop
If (folder! = NULL ){
Folder = folder. Items (); // returns the folderitems object
Folder = folder. Item (); // returns the folderitem object
Folder = folder. Path; // return path
If (Folder. charat (Folder. Length-1 )! = ""){
Folder = folder + "";
}
Document. getelementbyid (PATH). value = folder;
Return folder;
}
}
Catch (e ){
Alert (E. Message );
}
}
When using:
<TD>
<Input type = "text" name = "path"/>
</TD>
<TD>
<Input type = "button" onclick = "browsefolder ('path ')"
Value = "select the generated path"/>
</TD>
2. solution 2:
Write a JS selection box to read the local hard disk. The disadvantage is that it looks worse than the previous one.
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
</Head>
<Body>
<Table border = "0" cellpadding = "0" width = "100%" id = "tb_show">
<Tr>
<TD width = "18%"> file storage location: </TD>
& Lt; TD width = "82%" & gt;
<% -- <HTML: file property = "file" size = "40" styleclass = "inputbox"/> -- %>
<Input name = "backdir" type = "text" value = "C:" size = "100" width = "500">
</TD>
</Tr>
<Tr>
<TD> directory location: </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"> Note: Double-click an option in the list box to display the folders in the list box. The first is the root directory. </font>
</TD>
</Tr>
</Table>
</Body>
</Html>
<SCRIPT>
/**//*
* Initialize and put all the system drivers into the table_drives list.
*/
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 = "[drive not ready]";
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 );
}
}
/**//*
* Put all the folders on the drive selected in the tables_drives list into the table_folder list.
*/
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)
{< br> tablefolders. options. remove (LEN);
Len --;
}< br> var option = document. createelement ("option");
option. value = drive + ":";
option. TEXT = drive + ":";
tablefolders. add (option);
var folders = S. split (",");
for (j = 0; j {< br> option = document. createelement ("option");
optio N. value = folders [J];
option. TEXT = folders [J];
tablefolders. add (option);
}< BR >}< br> else
{< br> alert ("the current content cannot be changed! ")
}< BR >}< br> else
return false;
}< BR >}
/** // *
* double-click an option in table_folder to display the folders in the table_folder list.
*/
function get_file ()
{< br> var tablefolders = document. getelementbyid ("table_folder");
var tabledrives = document. getelementbyid ("tables_drives");
for (VAR I = 0; I {< br> If (tablefolders. options [I]. selected = true)
{< br> var FSO, F, FC, s;
var folderpath = tablefolders. options [I]. value. substring (0, tablefolders. options [I]. value. length);
If (folderpath. charat (folderpath. length-1) = "")
{< br> document. getelementbyid ("backdir "). value = folderpath;
}< br> else
{< br> document. getelementbyid ("backdir "). value = folderpath + "";
}
FSO = new activexobject ("scripting. fileSystemObject ");
F = FSO. getfolder (folderpath);
fc = new enumerator (F. subfolders);
S = "";
(;! FC. atend (); FC. movenext ()
{< br> S + = FC. item ();
S + = ",";
}< br> var Len = tablefolders. options. length;
while (LEN> = 0)
{< br> tablefolders. options. remove (LEN);
Len --;
}< br> var opt = "";
var opt1 = "";
for (j = 0; j {< br> var option = document. createelement ("option");
opt = opt + folderpath. split ("") [J] + "";
If (j> 0)
{< br> opt1 = OPT;
option. value = opt1.substring (0, opt1.length-1);
option. TEXT = opt1.substring (0, opt1.length-1);
tablefolders. add (option);
}< br> else
{< br> 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>