Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

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

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 computer.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 comment: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...