Javascript select folder

Source: Internet
Author: User

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 following code to apply the 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"; // The selection box prompts
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 your own 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)
{
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 ("the current content cannot be changed! ")
}
}
Else
Return false;
}
}
}

/**//*
* Double-click an option in the table_folder option to display the folders in the table_folder list.
*/
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.