javascript Javascript列出网络服务器目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9057931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript to list all files in directory on the webserver
提问by Reily Bourne
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.
我知道如何在服务器端语言中做到这一点,但我想知道是否有一种简单的方法(如没有活动的 X)。Google 搜索只为我提供了让 javascript 列出用户计算机中文件的方法。
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.
如何使用 javascript 列出服务器上的所有文件。也就是说,如果我有一个文件夹 /gallary 并且里面可以有很多子导演,/gallery/sports、/gallery/california、/gallery/christmas。每个子目录包含 n 个图像。
How can I get javascript to list all sub-directories as well as all the images.
如何让 javascript 列出所有子目录以及所有图像。
采纳答案by Matthew Lock
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 answer: https://stackoverflow.com/a/9057959/74585
由于浏览器中的 javascript 无法直接访问服务器的文件系统,您可能需要一些服务器端脚本,例如 PHP、Perl、ASP 等将文件系统内容发送到网页(可能通过 Ajax),然后使用 javascript 格式将文件系统内容转换为所需格式,例如使用此答案中提到的文件树控件:https: //stackoverflow.com/a/9057959/74585
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.
如果您不能使用任何服务器端脚本,那么也许您可以对 javascript 文件中的类别进行硬编码(假设类别更改不经常),并按顺序对图像进行编号?然后,您的 javascript 可以通过尝试加载图像类别文件夹和编号来查找图像。然后通过 onerror 检测图像何时未加载并停止显示上一个/下一个按钮。
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.
一个更左的不需要服务器端脚本的解决方案可能是在通过 FTP 连接的工作站机器上创建一个脚本,比如 perl,查看所有文件夹和文件,并创建一个 JSON 或 XML 文件,其中包含文件系统的内容。然后您的 javascript 可以调用该生成的文件并访问文件系统。缺点是每次要添加另一个文件时都需要重新运行工作站脚本。