rustwebassembly

Can I read files from the disk by using Webassembly? (re-evaluated)


what's currently possible in webassembly running in the browser regarding accessing the local file system? There's an older question that says it's not possible due to security restrictions. But some places (e.g. https://fjolt.com/article/javascript-new-file-system-api) say that there's a new file system API available in some browsers that would allow that.

a) what needs to be done to be able to use that api? b) can I use it from rust with webassembly - and how?

Thank you for any useful pointer. Tobias


Solution

  • The current web APIs (https://developer.mozilla.org/en-US/docs/Web/API) only lets you access files in the local files system by prompting the user to open or save files. These APIs are not (yet) accesible from webassembly directly. So what you have to do is

    1. Use javascript to prompt the user to open a file (or directory)
    2. Grab the contents of the file and pass it to your webassembly code
    3. Process the contents with webassembly (or in your case rust compiled to webassembly)
    4. Pass the processed content back to javascript that can prompt the user to save the file.

    or some variation of the theme above. Read more about webassmbly usage here https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API

    and Rust and webassembly e.g. here

    https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm