Javascript:列出文件和文件夹结构 [英] Javascript: Listing File and Folder Structure
问题描述
- 可以在客户端PC上列出JavaScript文件和文件夹结构?
- 可以在服务器上列出JavaScript文件和文件夹结构吗?
如果您的回答是不,Java Scipt无法在客户端或服务器的C:\驱动器上列出文件和文件夹,而CGI脚本是唯一的解决方案吗?
浏览器JS读取客户端PC的文件:取决于
出于安全原因,未经用户同意,您无法访问用户PC上的文件。
这就是为什么 FileReader API 是在文件输入框< input type =file>
和一个拖放区域周围创建的,因为整个想法是访问访问该文件经用户同意。如果用户没有故意放置文件进行访问,则根本无法访问它。
服务器端JS读取自己服务器的文件:是
至于服务器,如果您的意思是使用server-JS( NodeJS 或 Rhino ),是的,你可以(无论如何还能为网页提供服务吗?)。
浏览器JS读取自己服务器的文件:取决于
使用JS从浏览器访问服务器 你有一个API来读取它的文件。
浏览器JS读取其他服务器的文件:是的,有一个捕获
<要访问其他服务器的文件而不使用某些API,您可以采取措施创建一个运行服务器端的web抓取工具或web-spider(因为浏览器无法通过同源政策)并将API暴露给您的浏览器。
然而:
- 您无法抓取所有文件,因为有些人可能从外部访问中受到限制。
- 结构的公开外观可能与内部结构不同,特别是如果网站使用细分网址方案
- 使用查询字符串生成页面的网站由于其可能产生的排列数量而无法轻易抓取,因此某些网页可能无法缓存。
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:
- Can JavaScript list files and folder structure on a client PC?
- Can JavaScript list files and folder structure on a server?
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?
Browser JS reading client PC's files: Depends
For a security reason, you can't access the files on the user's PC without the user's consent.
That's why the FileReader API is 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.
Server-side JS reading own server's files: Yes
As for server, if you meant access the server using server-JS (NodeJS or Rhino), yes you can (How else would it serve webpages anyway?).
Browser JS reading own server's files: Depends
Accessing the server from the browser using JS works if you have an API to read files from it.
Browser JS reading other server's files: Yes, with a catch
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.
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.
这篇关于Javascript:列出文件和文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!