仅使用 javascript 列出目录中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2924935/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:31:48  来源:igfitidea点击:

List files in a directory using only javascript

javascript

提问by Anthony Garand

Is it possible to list the files in a directory using only javascript? To clarify, I mean list the files on the server, not the files on the clients computer. For instance:

是否可以仅使用 javascript 列出目录中的文件?为了澄清,我的意思是列出服务器上的文件,而不是客户端计算机上的文件。例如:

www.domain.com/files/

www.domain.com/files/

contains 4 images (.jpg)

包含 4 张图片 (.jpg)

Can I make an extra page (www.domain.com/files/list.html) that lists those 4 files using javascript?

我可以制作一个额外的页面 (www.domain.com/files/list.html) 使用 javascript 列出这 4 个文件吗?

采纳答案by Tatu Ulmanen

No, Javascript doesn't have access to the filesystem. Server side Javascriptis a whole different story but I guess you don't mean that.

不,Javascript 无权访问文件系统。服务器端 Javascript是一个完全不同的故事,但我想你不是那个意思。

回答by Heather Gaye

Very late to this party, but my google search for this exact request led me here.

这个派对很晚了,但我对这个确切请求的谷歌搜索把我带到了这里。

The answer is "not really", but I've found the frankenstein of hacks elsewhere: If +Indexes is (or can be) enabled in the .htaccess for the folder containing the files you want to list, then use XMLHTTPRequest with the folder name as the url (which will return an html page listing the files).

答案是“不是真的”,但我在其他地方发现了黑客的弗兰肯斯坦:如果在 .htaccess 中为包含您要列出的文件的文件夹启用了 +Indexes,则将 XMLHTTPRequest 与该文件夹一起使用name 作为 url(这将返回一个列出文件的 html 页面)。

回答by microspino

I don't know if you architecture allows it but ikf you can install and use node.js as its node APImentions, you can interact with the filesystem by requiring the fs module.

我不知道你的架构是否允许它,但是 ikf 你可以安装和使用 node.js 作为它的节点 API提到的,你可以通过要求 fs 模块与文件系统交互。

This is the environment Node.js relies on:

这是 Node.js 依赖的环境:

Node eventually wants to support all POSIX operating systems (including Windows with MinGW) but at the moment it is only being tested on Linux, Macintosh, and Solaris. The build system requires Python 2.4 or better. V8, on which Node is built, supports only IA-32 and ARM processors. V8 is included in the Node distribution. To use TLS, OpenSSL are required. There are no other dependencies.

Node 最终希望支持所有 POSIX 操作系统(包括带有 MinGW 的 Windows),但目前仅在 Linux、Macintosh 和 Solaris 上进行测试。构建系统需要 Python 2.4 或更高版本。构建 Node 的 V8 仅支持 IA-32 和 ARM 处理器。V8 包含在 Node 发行版中。要使用 TLS,需要 OpenSSL。没有其他依赖项。

You can run It side-by-side with another web app. and this will avoid blocking your web application if the interaction with the filesystem takes too long.

您可以与另一个 Web 应用程序并排运行它。如果与文件系统的交互时间过长,这将避免阻塞您的 Web 应用程序。

回答by microspino

It is generally not a good idea to access client computer files via javascript for securityreasons, however i suspect you can use the File System Objectfor that. I am not sure about browser-compatibility for that, it should work in IE only probably though.

出于安全原因,通过 javascript 访问客户端计算机文件通常不是一个好主意,但是我怀疑您可以为此使用文件系统对象。我不确定浏览器的兼容性,不过它应该只能在 IE 中工作。

You need to use server-side languages such as PHP, ASP.Net, JSP, etc

您需要使用服务器端语言,例如PHP、ASP.NetJSP

回答by Andy West

JavaScript runs inside a host environment. So if the host provides a facility to list files in this manner, then yes. But in the typical scenario where JavaScript is running in a browser with default configuration, no.

JavaScript 在宿主环境中运行。因此,如果主机提供了以这种方式列出文件的工具,那么是的。但在 JavaScript 运行在具有默认配置的浏览器中的典型场景中,没有。