plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.


Please take a moment to read the platform’s guidelines before you get started!


question

Steve avatar image
Steve asked davux3 commented

Is it possible to read files in the Work directory from JavaScript?

I'm currently investigating building an in game panel which interfaces with an in-proc wasm module and as part of this have a reasonable amount of local user data (potentially a few KB) to read on the HTML/JS side.

Is it possible to read files saved in the work folder from JavaScript to save me sending this in tiny chunks over L-Vars?

If it isn't possible right now, could this be supported in the future?

html
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

FlyingRaccoon avatar image
FlyingRaccoon answered

Hello Steve,

As you mentioned in your last comments, only files embedded in the package can be accessed through js at the moment.
I've added a task about giving access to the work directory in our backlog.
I suggest you create an idea about this feature so people can upvote it and we have a better idea of how to prioritize it.

Regards,
Sylvain

10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

evanburnsdev avatar image
evanburnsdev answered davux3 commented

I haven't tried file access in javascript with the sim yet, so just a shot in the dark:

let rawFile = new XMLHttpRequest();
rawFile.open("GET", "\work\filename.txt", true);
rawFile.onreadystatechange = function() {
  if (rawFile.readyState === 4) {
    this.allText = rawFile.responseText;
  }
}
4 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

Steve avatar image Steve commented ·

You also need to call rawFile.send() after building the request, but unfortunately that results in the following error:

[Error] Failed to load resource: the server responded with a status of 404 (Unable to process resource request) (work\test.json, line 0)

For what it's worth, the code snippet you provided is essentially what Utils.loadFile does internally. I tried a few different variants of the path with no success:

/work/test.json        (404)
./work/test.json       (404)
coui://work/test.json  (Cross origin request error)
work://test.json       (Cross origin request error)
0 Likes 0 ·
Steve avatar image Steve Steve commented ·
Just to add for the benefit of others, this method works perfectly fine for files that are embedded in the package, but files written to the work folder from a wasm module can't be read in this way unless there's another path/API that I haven't found yet.
0 Likes 0 ·
B21 avatar image B21 Steve commented ·

necro alert, apologies - "this method works perfectly fine for files that are embedded in the package" - is that correct?

In my JS I'm trying to READ files in the SimObject directory of my package and as far as I can tell Coherent is treating the "html_UI" package directory as the root of the http access, so there's no access in JS outside of that directory (I've tried "/../SimObjects/...." but no success).

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.