Javascript:列出文件和文件夹结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10774377/
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: Listing File and Folder Structure
提问by Bunkai.Satori
As I have read, it is not easy for JavaScript to modify files on client PC. I am working on a web based file manager and would need to know the following:
正如我所读到的,JavaScript 修改客户端 PC 上的文件并不容易。我正在开发基于 Web 的文件管理器,需要了解以下内容:
- Can JavaScript list files and folder structure on a client PC?
- Can JavaScript list files and folder structure on a server?
- JavaScript 可以列出客户端 PC 上的文件和文件夹结构吗?
- JavaScript 可以列出服务器上的文件和文件夹结构吗?
If your answer is no, that Java Scipt can not list files and folders say on client's or server's C:\ drive, than would CGI script be the only solution?
如果您的答案是否定的,Java Scipt 无法列出客户端或服务器的 C:\ 驱动器上的文件和文件夹,那么 CGI 脚本将是唯一的解决方案吗?
回答by Joseph
Browser JS reading client PC's files: Depends
浏览器 JS 读取客户端 PC 的文件:取决于
For a security reason, you can't access the files on the user's PC without the user's consent.
出于安全原因,未经用户同意,您无法访问用户 PC 上的文件。
That's why the FileReader APIis created around the file input box <input type="file">
and a drag-n-drop area since the whole idea is to "access the file with user's consent". Without the user intentionally putting the file for access, you can't access it at all.
这就是围绕文件输入框和拖放区域创建FileReader API的原因,<input type="file">
因为整个想法是“在用户同意的情况下访问文件”。如果用户没有故意放置文件以供访问,则您根本无法访问它。
Server-side JS reading own server's files: Yes
服务器端 JS 读取自己服务器的文件:是
As for server, if you meant access the server using server-JS (NodeJSor Rhino), yes you can (How else would it serve webpages anyway?).
至于服务器,如果您的意思是使用 server-JS(NodeJS或Rhino)访问服务器,是的,您可以(无论如何它会为网页提供服务?)。
Browser JS reading own server's files: Depends
浏览器 JS 读取自己服务器的文件:取决于
Accessing the server from the browserusing JS works if you have an API to read files from it.
如果您有一个 API 可以从中读取文件,则可以使用 JS从浏览器访问服务器。
Browser JS reading other server's files: Yes, with a catch
浏览器 JS 读取其他服务器的文件:是的,有一个问题
To access other server's files without some API, you could resort to creating a web scraper or a web-spider that runs server-side(since browser can't cross domains due to the same origin policy) and have an API exposed to your browser.
要在没有 API 的情况下访问其他服务器的文件,您可以求助于创建一个运行服务器端的网络爬虫或网络蜘蛛(因为浏览器由于同源策略而无法跨域)并将 API 暴露给您的浏览器.
However:
然而:
- you can't crawl to all files as some may be restricted from outside access.
- the public appearance of the structure could be different from the internal structure, especially if the site uses segmented url scheme
- sites using query strings to generate pages cannot be crawled easily due to the number of permutations it could make, thus some pages might be unreacheable.
- 您无法抓取所有文件,因为有些文件可能会受到外部访问的限制。
- 结构的公开外观可能与内部结构不同,特别是如果站点使用分段 url 方案
- 使用查询字符串生成页面的站点由于可能产生的排列数量而无法轻松抓取,因此某些页面可能无法访问。
回答by lanzz
CGI won't be a solution either, as it only has access to the filesystem of your server, not that of the client visiting your site. The only way to access your client's filesystem from javascript seems to be the File API, which apparently is not implemented by many browsers.
CGI 也不是解决方案,因为它只能访问您服务器的文件系统,而不能访问访问您站点的客户端的文件系统。从 javascript 访问客户端文件系统的唯一方法似乎是File API,显然许多浏览器都没有实现。
回答by Mainmeister
It's a cludge but you could resort to a java applet or the dreaded active-x control.
这是一个障碍,但您可以求助于 Java 小程序或可怕的 active-x 控件。