Manipulating the local file system with browser-based JavaScript and Node

前端 未结 2 1776
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 09:20

I am making a project that needs to allow users to interact with the file system from a browser. I have a lot of experience writing client-side JavaScript, and I have a lot

2条回答
  •  情深已故
    2020-12-03 09:39

    Naturally, browser-hosted JavaScript cannot access the file system of the user's machine (at present; someday, some kind of sandboxed access may happen — the last attempt failed, but that doesn't mean the next one has to).

    So to do this, you'll need two pieces:

    1. A browser piece, which does the UI with the user.

    2. A Node piece, which runs on the user's machine (and thus can access the file system) and which the browser piece uses to do the actual file operations.

    Probably the easiest way for the pieces to interact would be HTTP, which you can trivially support using ExpressJS.

    So for instance, if the user wants to delete a file:

    1. User clicks something to say "delete this file"
    2. Browser JavaScript sends the command to the Node process over HTTP via ajax
    3. Node process does the deletion and reports success/failure
    4. Browser JavaScript displays the result

提交回复
热议问题