/*** Browsefolder. JS * this file defines the browsefolder () function, which will provide a folder selection dialog box * for users to select the system folder function * folder selection dialog box starting directory by * shell. browseforfolder (window_handle, message, options, strpath) function * strpath parameter settings * For example: 0x11 -- my computer * 0 -- desktop * "C: // "-- System Drive C ** is used as follows Code Apply this function to an HTML file: * <SCRIPT src = "browsefolder. JS "> </SCRIPT> * or copy the following code directly to <script language =" JavaScript ">... </SCRIPT> label. ** common trigger functions can be used in HTML files. * example: <input type = "button" onclick = "browsefolder () "/> * Note: Please define the input box named" savepath "to receive or display the returned value, for example, * <input type =" text "name =" savepath "/> ** 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. Set a trusted site security level: set the following option * "initialize ActiveX controls that are not marked as secure and run scripts" ---- "enable" */function browsefolder () {try {var message = "select a folder"; // The message var shell = new activexobject ("shell. application "); var folder = shell. browseforfolder (0, message, 0 x, 0x11); // start Directory: My computer // var folder = shell. browseforfolder (0, message, 0); // The starting directory is: 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. all. savepath. value = folder; document. getelementbyid ("savepath "). value = folder; // savepath indicates the Output Control ID. Return folder ;}} catch (e) {alert (E. Message );}}
Another way to avoid security issues is to use js to implement folder selection, which will be ugly.
<Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <title> untitled document </title> > </TD> </tr> <TD> directory location: </TD> <select name = "Tables_drive" id = "tables_drives" onchange = "get_drives ()" > </SELECT> </TD> </tr> <TD colspan = "2"> <Select name = "Table_folder" id = "table_folder" size = "10" multiple ondblclick = "get_file ()" > </SELECT> </TD> </tr> </table> </body> "Scripting. FileSystemObject" ); E = new enumerator (FSO. Drives); s = "" ; (;! 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 = "" ; (;! 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 = "" ; (;! 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>