...

View Full Version : JAVA Script



ramyaid
09-19-2005, 01:16 PM
How to select a folder from html file dialogue box using Java Script, Jsp or Java

nikkiH
09-19-2005, 01:58 PM
JScript, Windows MSIE only.
getFolder (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthgetfolder.asp)

ramyaid
09-20-2005, 05:10 AM
I need a dialogue box like file dialogue box, to select folder. The answer u gave is different.

glenngv
09-20-2005, 05:32 AM
There's no built-in HTML tag or script to browse folders. You will have to script it manually.
And the method that nikkiH mentioned is one of the methods you will need.
Actually, what you asked for is a complicated script, because aside from all the list of drives, folders, network folders, etc., you need to display them in a tree view.

The closest you can get without scripting is by using the file input which can browse local files.


<input type="file" name="file1" />

ramyaid
09-20-2005, 05:55 AM
Hi ,

Since i need it ASAP. I know that it can be achieved only through script. but can any one help me to get the code ASAP. That is what I want.

TNO
09-20-2005, 09:08 AM
How about this?


function GetFolder(){
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject")
var BIF_EDITBOX = 0x10;
var BIF_NEWDIALOGSTYLE = 0x40;
var sa
sa = new ActiveXObject("Shell.Application");
var oF;
oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX || BIF_NEWDIALOGSTYLE);
var fi;
fi = oF.Items.Item;
}

nikkiH
09-20-2005, 02:29 PM
Don't quote me, but I think

sa = new ActiveXObject("Shell.Application");

gives security problems with WinXP SP2 unless the site is in the trusted site list.
Not sure.
If anyone knows for sure, please post. :D

felgall
09-20-2005, 10:42 PM
I am sure it will give security problems with SP2. Web pages are not supposed to be able to access the local computer unless it is an intranet site and so only the local site should have local access. Microsoft recommend disabling ActiveX in IE running on older operating systems for the same reason.

gph
09-21-2005, 12:31 AM
Here is pretty much the same thing TNO posted with some fixes to problems I've come across.

As already stated, this is only usable offline.



var fso=new ActiveXObject('Scripting.FileSystemObject');

function folder_dialogue(instruction_text){
var dir=new ActiveXObject('Shell.Application').BrowseForFolder(0,instruction_text,0x0001);
if(dir){
dir=dir.ParentFolder?dir.Items().Item().Path:new ActiveXObject('WScript.Shell').SpecialFolders('Desktop');
if(dir.charAt(dir.length-1)!='\\')dir+='\\';
if(fso.FolderExists(dir))return dir
}
return false
}


alert(folder_dialogue('some text'))


Here's the MS BrowseForFolder (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/shell/browseforfolder.asp) page

ramyaid
09-21-2005, 05:26 AM
I thank every ones effort. The thing i need is some thing like this..

but i need a proper gui. like file dialogue box.. can any one help in this case..

<html>
<head>
<title>Browse Folders</title>
<script language="JavaScript">

var currentFolder='';
function GetDriveList(){
var fso, obj, n, e, item, arr=[];
try {
fso = new ActiveXObject("Scripting.FileSystemObject");
} catch(er) {
alert('Could not load Drives. The ActiveX control could not be started.');
cancelFolder();
}

e = new Enumerator(fso.Drives);
for(;!e.atEnd();e.moveNext()){
item = e.item();
obj = {letter:"",description:""};
obj.letter = item.DriveLetter;
if (item.DriveType == 3) obj.description = item.ShareName;
else if (item.IsReady) obj.description = item.VolumeName;
else obj.description = "[Drive not ready]";
arr[arr.length]=obj;
}
return(arr);
}
function GetSubFolderList(fld){
var e, arr=[];
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(fld.toString());
var e = new Enumerator(f.SubFolders);
for(;!e.atEnd();e.moveNext()){
arr[arr.length]=e.item().Name;
}
return(arr);
}
function loadDrives(){
var drives=GetDriveList(),list='';
for(var i in drives){
list+='<div onclick="loadList(\''+drives[i].letter+':\\\\\')" class="folders" onmouseover="highlight(this)" onmouseout="unhighlight(this)">'+drives[i].letter+':\\ - '+ drives[i].description+'</div>';
}
document.getElementById("path").innerHTML='<a href="" onclick="loadDrives();return false" title="My Computer">My Computer</a>\\';
document.getElementById("list").innerHTML=list;
currentFolder='';
}
function loadList(fld){
var path='',list='',paths=fld.split("\\");
var divPath=document.getElementById("path");
var divList=document.getElementById("list");
for(var i=0;i<paths.length-1;i++){
if(i==paths.length-2){
path+=paths[i]+' \\';
}else{
path+='<a href="" onclick="loadList(\'';
for(var j=0;j<i+1;j++){
path+=paths[j]+"\\\\";
}
path+='\');return false">'+paths[i]+'</a> \\ ';
}
}
divPath.innerHTML='<a href="" onclick="loadDrives();return false">My Computer</a> \\ '+path;
divPath.title="My Computer\\"+paths.toString().replace(/,/g,"\\");
currentFolder=paths.toString().replace(/,/g,"\\");

var subfolders=GetSubFolderList(fld);
for(var j=0;j<subfolders.length;j++){
list+='<div onclick="loadList(\''+(fld+subfolders[j]).replace(/\\/g,"\\\\")+'\\\\\')" onmouseover="highlight(this)" onmouseout="unhighlight(this)" title="'+subfolders[j]+'" class="folders">'+subfolders[j]+"</div>";
}
divList.innerHTML=list;
resizeList();
divPath.scrollIntoView();
}
function resizeList(){
var divList=document.getElementById("list");
var divPath=document.getElementById("path");
if(document.body.clientHeight>0 && divPath.offsetHeight>0){
divList.style.height=document.body.clientHeight-divPath.scrollHeight;
}
}
function highlight(div){
div.className="folderButton";
}
function unhighlight(div){
div.className="folders";
}
function selectFolder(){
window.returnValue=currentFolder;
window.close();
}
function cancelFolder(){
window.returnValue='';
window.close();
}

function GetFolder(){
alert("hi");
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject")
var BIF_EDITBOX = 0x10;
var BIF_NEWDIALOGSTYLE = 0x40;
var sa;
sa = new ActiveXObject("Shell.Application");
var oF;
oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX || BIF_NEWDIALOGSTYLE);
var fi;
fi = oF.Items.Item;
}
</script>

<style>
#header{
background-color: #CCCCCC;
border-bottom: solid 1px black;
}
#path{
position:relative;
font-size: 8pt;
font-family: Arial;
font-weight: bold;
padding: 2px;
}
#list{
font-size: 10pt;
font-family: Arial;
overflow:auto;
}
.folders{
padding: 1px;
border-top: solid 1px white;
border-left: solid 1px white;
border-right: solid 1px white;
border-bottom: solid 1px black;
cursor: hand;
pointer: hand;
background-color: white;
}
.folderButton{
padding: 0px;
border-style: outset;
border-width: 2px;
border-color:;
cursor: hand;
pointer: hand;
background-color: #CCCCCC;
}
A{
color:blue;
text-decoration:none;
padding:3px;
}
A:hover{
background-color: #CCCCCC;
padding:1px;
border-style: outset;
border-width: 2px;
}
</style>
</head>
<body onload="loadDrives()" onresize="resizeList()" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" scroll=no>
<form>
<div id="list">You must allow the ActiveX control to run in order to use this dialog.</div>
<div id="container">
<table border="0" cellpadding="0" cellspacing="0" id="header">
<tr>
<td align="right" width="1%" nowrap>
<!-- <input type="button" value="Select" onclick="selectFolder()"> -->
<input type="button" value="Select" onclick="GetFolder()">
<input type="button" value="Cancel" onclick="cancelFolder()">
</td>
<td><div id="path"></div></td>
</tr>
</table>
</div>
</form>
</body>
</html>

glenngv
09-21-2005, 06:44 AM
I modified the sample code from BrowseForFolder page in MSDN that gph linked. The BrowserForFolder() method has already the GUI. I modified it to get the full path of the selected folder.


<script language="JScript">
function fnShellBrowseForFolderJ()
{
var objShell = new ActiveXObject("Shell.Application");
var ssfCONSTANTS = 17; //ssfDRIVES; see this for more info of constants - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/shell/shellspecialfolderconstants.asp
var objFolder;

objFolder = objShell.BrowseForFolder(0, "Browse Folder Example", 0, ssfCONSTANTS);
if (objFolder != null)
{
var fol, path = objFolder.title;
var colon = path.indexOf(":");
if (colon != -1){
path=path.substr(colon-1,2);
}
else {
while (fol = objFolder.parentFolder){
colon = fol.title.indexOf(":");
if (colon!=-1){
path=fol.title.substr(colon-1,2) + "\\" + path;
break;
}
else path = fol.title + "\\" + path;
objFolder=objFolder.parentFolder;
}
}
if (path.indexOf("Desktop")!=-1){ //My Computer or Control Panel is selected
alert("Please select a drive or a folder.");
}
else{
alert("Selected path: "+path);
}
}
}

ramyaid
09-22-2005, 10:42 AM
if i use the browsefolder function or fnShellBrowseForFolderJ function i am getting script error. y is it so.

glenngv
09-22-2005, 10:56 AM
What error message? Can you post the code you're using?

iota
09-30-2005, 11:49 AM
What error message? Can you post the code you're using?

I also faced one.





Object doesn't support this property




But when I save the files as .hta , they perfectly run.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum