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

前端 未结 2 1775
佛祖请我去吃肉
佛祖请我去吃肉 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
    0 讨论(0)
  • 2020-12-03 09:41

    You cannot do this purely in Javascript. Javascript running on browsers does not have enough permission yet (there have been proposals) due to security reasons.

    https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API/Introduction#restrictions

    Because the file system is sandboxed, a web app cannot access another app's files. You also cannot read or write files to an arbitrary folder (for example, My Pictures and My Documents) on the user's hard drive.

    0 讨论(0)
提交回复
热议问题