jquery - Javascript to list all files in directory on the webserver - Stack Overflow

I know how to do this in server side languages but I was wondering if there is an easy way (as in no ac

I know how to do this in server side languages but I was wondering if there is an easy way (as in no active X). Google searches only give me ways for javascript to list files from the user's puter.

How can I use javascript to list all files on the server. That is, if I have a folder /gallary and there can be many sub-directors in there, /gallary/sports, /gallary/california, /gallary/christmas. Each sub-directory contains n image.

How can I get javascript to list all sub-directories as well as all the images.

I know how to do this in server side languages but I was wondering if there is an easy way (as in no active X). Google searches only give me ways for javascript to list files from the user's puter.

How can I use javascript to list all files on the server. That is, if I have a folder /gallary and there can be many sub-directors in there, /gallary/sports, /gallary/california, /gallary/christmas. Each sub-directory contains n image.

How can I get javascript to list all sub-directories as well as all the images.

Share Improve this question edited Jan 29, 2012 at 23:56 John Saunders 162k26 gold badges252 silver badges402 bronze badges asked Jan 29, 2012 at 23:55 Reily BourneReily Bourne 5,33710 gold badges33 silver badges44 bronze badges 6
  • 3 You can't. JavaScript is running on a different puter, and certainly doesn't have access to the file system of the server. – John Saunders Commented Jan 29, 2012 at 23:57
  • I do not think that due to server security you are able to do that. Javascript is client side and is executed client side. – Mike Commented Jan 29, 2012 at 23:58
  • 2 You could do it with the javascript only if the server generates index page with the list of the files in the directory. Otherwise you need server-side script to generate such list to give it back to javascript by request. – Cheery Commented Jan 30, 2012 at 0:13
  • 1 @Cheery Currently that is what my server does. How can you go about doing it this way? – Reily Bourne Commented Jan 30, 2012 at 0:34
  • 2 @JoshWeissbock Make ajax request to the folder, server will output the listing, which will be returned as a string to javascript. Than you have to parse it and output any way you want. – Cheery Commented Jan 30, 2012 at 0:41
 |  Show 1 more ment

1 Answer 1

Reset to default 2

As JavaScript in the browser cannot access the server's file system directly you are probably going to need some server side scripting such as PHP, Perl, ASP etc to send the file system contents to a web page (perhaps via Ajax) and then have JavaScript format the file system contents into the desired format, say using a file tree control mention in this ment: Javascript to list all files in directory on the webserver

If you can't use any server side scripting then perhaps you could hardcode the categories in the JavaScript file (assuming the categories change not very often), and number the images sequentially? Then your JavaScript can just go looking for images by trying to load an image category folder and number. Then detect when an image doesn't load via onerror and stop display previous/next buttons.

An even more left field solution which doesn't require server side scripting could be to create a script, say with Perl, on your workstation machine which connects over FTP, looks through all the folders and files and creates a JSON or XML file containing the contents of the file system. Then your JavaScript can call that generated file and get access to the file system. The drawback being that you need to rerun your workstation script each time you want to add another file.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744250846a4565159.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信