4

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?

0

3 Answers 3

8

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.
Sign up to request clarification or add additional context in comments.

7 Comments

+1, Hi Joseph. Than you for your detailed answer. You said: "Accessing the server from the browser using JS works if you have an API to read files from it.". Would you be so kind and advice some API to list the files and directory structure from the server, please?
@Bunkai.Satori just create methods that operate on files that are callable via GET/POST requests. also, you have to have some sort of user access control to limit only users with access rights to access your server's files.
you mean creating a CGI script, say in C++ that will manage local filesytem through GET/POST?
@Bunkai.Satori I don't know about CGI but yes, a server script that acts as an interface between the files system and browser over GET/POST. An example of that would be DropBox, where it lets users manage their online files over the internet.
I see.. thank you. What programming would you say seems to be best for this purpose? Can it be done with server side JavaScript too?
|
2

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.

4 Comments

I understand the security reasons. But then, is there a way to give users an area of disk space they could easily manage?
@HunterMcMillen Which is? I'm not denying that there is one, I just like it to know.
@panzi If browsers allowed implementations of Javascript that could alter files on client machines, it would be quite easy to steal personal information and also inject viruses into client machines. There are probably more reasons, those are the two that pop into my head.
@HunterMcMillen Well it does not work like this of course. It all happens in a sandboxed directory structure. The browser cannot access arbitrary files on the HD. But if your concern is, that the implementations might be buggy and there might be a way to escape the sandbox: Ok.
0

It's a cludge but you could resort to a java applet or the dreaded active-x control.

1 Comment

Hi, and thanks for your answer. You mentioned, the two methods. Java requires installing JRE, and active-x control limits usage to IE only. Is there any more accessible way, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.