views:

310

answers:

2

Hello all, I'm trying to read all filenames from a specified (server- not client) folder, and insert all the filenames into a javascript Array.
It should behave like the Directory.GetFiles method in ASP.NET C#.
I created a new array, and I just need the loop method (Javascript or jQuery) to iterate in the specific folder, and insert all the filenames into the array. Thanks all helpers!

A: 

You're going to need to invoke some form of ajax postback to do this on the server, returning an array of strings as a JSON result.

Note that this has the usual caveats that the web server account must have the necessary permissions to be able to query the selected folder.

Neil Moss
Hello Neil and thanks for your answer! Im trying to create a dynamic image gallery in jQuery that reads images filenames from a constant directory and loads them into a js array, but the filenames are dynamic and not constant. Is there any other way that you suggest me to do so ? Thank you very much!
Gal V
You will need to write a method in your web app which calls the Directory.GetFiles() method to enumerate your image filenames. This is C# code in a code-behind module. The method returns the resulting string array json-encoded.Use the jQuery.post() function (http://api.jquery.com/jQuery.post/) to invoke the method, specifying a datatype of "json". The data that is returned is a javascript version of the string array returned by the method above.Rick Strahl has a guide on linking asp.net to jQuery via ajax, here: http://www.west-wind.com/presentations/jquery/jquerypart2.aspx
Neil Moss
A: 

I wouldn't do this - it's potentially a security problem.

Out of interest, why are you doing this? I'm trying to imagine what you gain from having this information on the client...

Martin Milan
Gal V