Link to home
Start Free TrialLog in
Avatar of RayT
RayTFlag for United States of America

asked on

How do you read folder content using JavaScript?

How do I read the contents of a folder on the server into an array using JavaScript?
Avatar of black0ps
black0ps
Flag of United States of America image

But that's on the client side. Javascript doesn't work on the server side. You need a server side scripting language to do that, php, asp, cfm, etc.
http://javascript.internet.com/miscellaneous/computer-drive-browser.html

but as black0ps said, this will brows the client folder not server folder. if you can tel us why u need this we may help you more.
Javascript is a client side ONLY scripting language with is only active on client browser. It's impossible to do what you want.
And even though it's at client side, due to security issues, javascript also cannot read files on client's local file system.
>>Javascript is a client side ONLY scripting language

Utter nonsense. I use js on both the server and the client all the time, no problem.

You can read the folder content with the FileSystemObject and pass the (string) back to the client via Ajax.
>>I use js on both the server and the client all the time, no problem.

How? If you are talking about AJAX it's not pure javascript, just trigger event from client to server side. You should clear up your logic.

And the FileSystemObject is only supported by IE as using it is a bad practice and no other browsers support it.
>>How?

I have been using js on the server since about 1996.


>>You should clear up your logic.

I do not know what you mean.


>>And the FileSystemObject is only supported by IE

If the FSO is an IE-only thing, then I guess none of my web apps should be working. Maybe it is an IIS thing?


>>...using it is a bad practice

I do not agree.
You are talking about server-side Javascript, we are talking about client-side Javascript. Two things are different.
Avatar of Vel Eous
Vel Eous

Try the following:

<script language="javascript">

var my_js_array = new Array();


if ( $dir = opendir ( '.' ) )
{
while ( $file = readdir ( $dir ) )
{
if ( $file != '.' && $file != '..' )
{
$my_php_array[] = $file;
}
}
}

closedir ( $dir );

sort ( $my_php_rray );

foreach ( $my_php_array as $element )
{
echo ( "my_js_array.push( '$element' );" );      
}

?>

</script>
>>You are talking about server-side Javascript, we are talking about client-side Javascript. Two things are different.

They are only different in your mind. javascript on the server is exactly the same as javascript on the client, only more so ;-)

All of my .ASP pages are written in javascript; I have never had a problem with them running on different browsers (because they are server-based, of course).

So, if you are using IIS as your server, the FSO is perfectly valid, and there is *NO* stigma attached to it.

Avatar of RayT

ASKER

Maybe I should clarify my request.

The website has the following folder structure:

Parent_Folder
        |_ Folder_B

I have files sitting in Folder_B.  I just want to fill an array with those file names.  I have done this using ASP.NET.  Is this possible using JavaScript?

Thanks, All
         
You cannot do that on the client, but you can on the server.

You can make an Ajax request for the values, and the server can get the file names and send them up to the client.

It is easier to pass a delimited string to the client, and upon receiving it, split it into an array.

And yes, you can do it all in javascript, but in two separate places.
ASKER CERTIFIED SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RayT

ASKER

That's great.  Is there a way to do this without forcing the user to install an ActiveX Control?  Some users may have a restriction that will not allow them to install an ActiveX Control.
Since it is running ON THE SERVER, as long as your server is running IIS, you need not install anything. You may have to reference the FSO from within your IDE, but that should be it.

The above javascript is SERVER-SIDE CODE, not client-side code.
Avatar of RayT

ASKER

Thanks!