Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Javascript: Listing File and Folder Structure

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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...