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 FlyingRaccoon answered

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 characters needed characters left characters exceeded

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 characters needed characters left characters exceeded

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 Steve 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;
  }
}
2 comments
10 |10000 characters needed characters left characters exceeded

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

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 ·
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 ·

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 4.8 MiB each and 23.8 MiB total.